Python Dynamic Language and Detailed Explanation of Duck Types

  • 2021-07-06 11:10:44
  • OfStack

Today, let's talk about dynamic typing languages and duck types in programming languages.

Dynamic language

Wikipedia's definition of dynamic language:

Dynamic programming languages are a class of languages that can change their structure at runtime: for example, new functions, objects, and even code can be introduced, existing functions can be deleted, or other structural changes can be made. At present, dynamic languages are very dynamic, such as PHP, Ruby and Python, but C, C + + and Java are not dynamic languages.

This explanation is very abstract. In fact, dynamic language is relative to static language. The characteristic of static language is that before the program is executed, you can know 1 cut from the code when the code is compiled, such as the type of variable and the return value type of method:


String s = "hello"
s = "world"
s = 1 //  An error will be reported at compile time 

In static language, variables have type information, which is a block of memory area. The advantage of static language is that the code structure is very standard, which is easy to debug, but sometimes it seems wordy. Dynamic languages don't know 1 cut until the program runs. Variables (strictly called names, just like people's names 1) do not need to specify types. Variables themselves do not have any type information. The type information is on objects. What type objects are must be known until the program runs. The advantage of dynamic type language is that it is convenient to read and does not need to write a lot of type-related codes; The disadvantage is that it is inconvenient to debug, and when the naming is not standardized, it will cause reading and understanding, which is not conducive to understanding.


s = "hello"
s = "world"
s = 1 #  You can assign values to variables at will, no matter what type 

Duck type

Duck type is often mentioned in dynamic language. The so-called duck type is: if it walks like a duck and barks like a duck, then it is a duck (If it walks like ES30duck and quacks like a duck, it must be ES40duck). Duck type is a design style of dynamic type language in programming languages. The characteristics of an object are not determined by the parent class, but by the method of the object.

If you are studying static languages such as Java or C + +, you may not have a deep understanding of duck types, because the characteristics of objects in static languages depend on their parent classes. Dynamic languages are different, such as iterators. Any object that implements __iter__ and __next__ methods can be called iterators, but the type of the object itself is not limited and can be customized to any class


# python3
class Foo:
def __iter__(self):
pass
def __next__(self):
pass
from collections import Iterable
from collections import Iterator
print(isinstance(Foo(), Iterable)) # True
print(isinstance(Foo(), Iterator)) # True

We don't need to inherit Iterator to realize the function of iterator. When a function 1 wants to receive parameters of Iterator type, but we pass an instance object of Foo, it is no problem. If it is a static language such as Java, we must pass Iterator or its subclass. Duck types usually benefit from "not" testing the types of parameters in methods and functions, Instead, it relies on documents, clear code and tests to ensure correct use, which is both an advantage and a disadvantage. The disadvantage is that the parameter type needs to be known through documents. To make up for this deficiency, Python3.6 introduces type information, and the type can be specified when defining variables, such as:


def greeting(name: str) -> str:
return 'Hello ' + name

This function accepts an argument of type str and returns a value of type str


Related articles: