Python 简明教程

Python - Dynamic Binding

object-oriented programming 中, dynamic binding 的概念与多态性紧密相关。在 Python 中,动态绑定是在运行时而不是在编译时解析方法或属性的过程。

In object-oriented programming, the concept of dynamic binding is closely related to polymorphism. In Python, dynamic binding is the process of resolving a method or attribute at runtime, instead of at compile time.

根据 polymorphism 特性,不同的对象会根据其实现对同一方法调用做出不同的响应。这种行为可以通过 method overriding 实现,其中一个子类提供了在其超类中定义的 method 的实现。

According to the polymorphism feature, different objects respond differently to the same method call based on their implementations. This behavior is achieved through method overriding, where a subclass provides its implementation of a method defined in its superclass.

Python interpreter 根据对象在运行时的类型或类层次结构确定调用哪个恰当的方法或 attribute 。这意味着要调用的特定方法或属性会根据对象的实际类型动态确定。

The Python interpreter determines which is the appropriate method or attribute to invoke based on the object’s type or class hierarchy at runtime. This means that the specific method or attribute to be called is determined dynamically, based on the actual type of the object.

Example

以下示例说明了 Python 中的动态绑定 −

The following example illustrates dynamic binding in Python −

class shape:
   def draw(self):
      print ("draw method")
      return

class circle(shape):
   def draw(self):
      print ("Draw a circle")
      return

class rectangle(shape):
   def draw(self):
      print ("Draw a rectangle")
      return

shapes = [circle(), rectangle()]
for shp in shapes:
   shp.draw()

它将生成以下 output

It will produce the following output

Draw a circle
Draw a rectangle

如你所见,draw() 方法会根据对象的类型动态绑定到相应的实现。这就是在 Python 中实现动态绑定的方式。

As you can see, the draw() method is bound dynamically to the corresponding implementation based on the object’s type. This is how dynamic binding is implemented in Python.

Duck Typing

另一个与动态绑定紧密相关的概念是 duck typing 。是否一个对象适合某个特定用途由它是否具有某些方法或属性决定,而不是由它的类型决定。这使得 Python 具有更高的灵活性,并能重用代码。

Another concept closely related to dynamic binding is duck typing. Whether an object is suitable for a particular use is determined by the presence of certain methods or attributes, rather than its type. This allows for greater flexibility and code reuse in Python.

Duck typing 是诸如 Python ( PerlRubyPHPJavascript 等) 之类的动态类型语言的一项重要特性,它关注的是对象的的行为而不是其具体类型。根据“鸭子类型”概念,“如果它的走路方式像鸭子,叫声也像鸭子,那么它一定是鸭子”。

Duck typing is an important feature of dynamic typing languages like Python (Perl, Ruby, PHP, Javascript, etc.) that focuses on an object’s behavior rather than its specific type. According to the "duck typing" concept, "If it walks like a duck and quacks like a duck, then it must be a duck."

Duck typing 允许不同类型只要具有所需方法或属性,就可以互换使用。目标是提高灵活性,并重复使用代码。这是一个更宽泛的概念,它强调的对象的行为和接口,而不是正式类型。

Duck typing allows objects of different types to be used interchangeably as long as they have the required methods or attributes. The goal is to promote flexibility and code reuse. It is a broader concept that emphasizes object behavior and interface rather than formal types.

这里是一个鸭子类型的示例 −

Here is an example of duck typing −

class circle:
   def draw(self):
      print ("Draw a circle")
      return

class rectangle:
   def draw(self):
      print ("Draw a rectangle")
      return

class area:
   def area(self):
      print ("calculate area")
      return

def duck_function(obj):
   obj.draw()

objects = [circle(), rectangle(), area()]
for obj in objects:
   duck_function(obj)

它将生成以下 output

It will produce the following output

Draw a circle
Draw a rectangle
Traceback (most recent call last):
 File "C:\Python311\hello.py", line 21, in <module>
  duck_function(obj)
 File "C:\Python311\hello.py", line 17, in duck_function
 obj.draw()
AttributeError: 'area' object has no attribute 'draw'

duck typing 背后的最重要思想是 duck_function() 并不关心它收到的对象的具体类型。它仅要求对象具有 draw() 方法。如果一个对象通过拥有必要的行为“像鸭子那样叫”,那么在调用 draw() 方法时,它将被视为“鸭子”。

The most important idea behind duck typing is that the duck_function() doesn’t care about the specific types of objects it receives. It only requires the objects to have a draw() method. If an object "quacks like a duck" by having the necessary behavior, it is treated as a "duck" for the purpose of invoking the draw() method.

因此,在鸭子类型中,焦点在于对象的的行为,而不是它的明确类型,这允许只要不同类型对象表现出所需的行为,就可以互换使用。

Thus, in duck typing, the focus is on the object’s behavior rather than its explicit type, allowing different types of objects to be used interchangeably as long as they exhibit the required behavior.