Home >Backend Development >Python Tutorial >Share examples of duck classes and polymorphism in Python

Share examples of duck classes and polymorphism in Python

零下一度
零下一度Original
2017-06-15 15:59:361410browse

The following editor will bring you an old-fashioned talk about duck classes and polymorphism in python. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look

1. What is polymorphism

##f35d6e602fd7d0f0edfa6f7d103c1b57One type has multiple types Ability

2cc198a1d5eb0d3eb508d858c9f5cbdbAllows different objects to react flexibly to the same message
5bdf4c78156c7953567bb5a0aef2fc53Treat each used object in a common way
23889872c2e8594e0f446a471a78ec4cNon-dynamic languages ​​must pass Implemented through inheritance and interfaces

2. Polymorphism in python

<1>通过继承实现多态(子类可以作为父类来使用)
<2>子类通过重载父类的方法实现多态

class Animal:
  def move(self):
    print(&#39;animal is moving....&#39;)
class Dog(Animal):
  pass
def move(obj):
  obj.move()

>>>move(Animal())
>>>animal is moving....
>>>move(Dog())
>>>animal is moving....

class Fish(Animal):
  def move(self):
    print(&#39;fish is moving....&#39;)
>>>move(Fish())
>>>fish is moving....

3. Dynamic language and Duck typing

f35d6e602fd7d0f0edfa6f7d103c1b57The type of variable binding is undefined


2cc198a1d5eb0d3eb508d858c9f5cbdbFunctions and methods can receive parameters of any type


5bdf4c78156c7953567bb5a0aef2fc53Do not check the provided parameter type when calling a method


23889872c2e8594e0f446a471a78ec4cWhether the call is successful is determined by the methods and attributes of the parameters. If the call is unsuccessful, it will throw An error occurred


43ad812d3a971134e40facaca816c822No need to implement the interface

class P:
  def init(self, x, y):
    self.x = x
    self.y = y
  def add(self, oth):
    return P(self.x+oth.x, self.y+oth.y)
  def info(self):
    print(self.x, self.y)
class D(P):
  def init(self, x, y, z):
    super.init(x, y)
    self.z = z

  def add(self, oth):
    return D(self.x+oth.x, self.y+oth.y, self.z+oth.z)
  def info(self):
    print(self.x, self.y, self.z)

class F:
  def init(self, x, y, z):
    self.x = x
    self.y = y
    self.z = z

  def add(self, oth):
    return D(self.x+oth.x, self.y+oth.y, self.z+oth.z)
  
  def info(self):
    print(self.x, self.y, self.z)
  

def add(a, b):
  return a + b

if name == &#39;main&#39;:
  add(p(1, 2), p(3, 4).info())
  add(D(1, 2, 3), D(1, 2, 3).info())
  add(F(2, 3, 4), D(2, 3, 4).info())

4. The benefits of polymorphism

< ;1> It can realize open expansion and closed modification

2cc198a1d5eb0d3eb508d858c9f5cbdbMake python program more flexible

The above is the detailed content of Share examples of duck classes and polymorphism in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn