Home Backend Development Python Tutorial A detailed introduction to Python's duck typing

A detailed introduction to Python's duck typing

Feb 23, 2017 am 11:18 AM

I believe that Python developers are familiar with Python’s duck typing. The accurate definition of duck typing in Wikipedia is ‘a style of dynamic typing. In this style, the effective semantics of an object are not determined by inheriting from a specific class or implementing a specific interface, but by the "current set of methods and properties". So this article gives you the duck typing of python.

Basic definition of duck type

First of all, Python does not support polymorphism, and it does not need to support polymorphism. Python is a kind of polymorphism. Language, advocating duck typing.

The following is a discussion of duck typing from Wikipedia:

In programming, duck typing (English: duck typing) is a style of dynamic typing. In this style, the effective semantics of an object are determined not by inheriting from a specific class or implementing a specific interface, but by the current set of methods and properties. The name of this concept comes from the duck test proposed by James Whitcomb Riley. The "duck test" can be expressed like this:

"When you see a bird that walks like a duck, swims like a duck, and quacks like a duck, If it looks like a duck, the bird can be called a duck."

In duck typing, the focus is not on the type of the object itself, but on how it is used. For example, in a language that doesn't use duck typing, we could write a function that takes an object of type duck and calls its walk and bark methods. In a language that uses duck typing, such a function can accept an object of any type and call its walk and call methods. If the methods that need to be called do not exist, a runtime error will be raised. The fact that any object with the correct walk and call methods can be accepted by a function leads to the above statement, hence the name of this way of determining types.

Duck typing often benefits from not testing the types of parameters in methods and functions, but instead relying on documentation, clear code, and testing to ensure correct usage. Users moving from statically typed languages ​​to dynamically typed languages ​​often attempt to add some static (before runtime) type checking, thereby compromising the benefits and scalability of duck typing and constraining the dynamic nature of the language.

Specific implementation in python

The following code is a simple duck type

class duck():
  def walk(self):
    print('I walk like a duck')
  def swim(self):
    print('i swim like a duck')

class person():
  def walk(self):
    print('this one walk like a duck') 
  def swim(self):
    print('this man swim like a duck')
Copy after login

For a duck type, we don't care about the type of the object itself or the inheritance of the class, but how the class is used. We can call the methods of these classes through the following code.

def watch_duck(animal):
  animal.walk()
  animal.swim()

small_duck = duck()
watch_duck(small_duck)

output >> 
I walk like a duck
i swim like a duck


duck_like_man = person()
watch_duck(duck_like_man)

output >> 
this one walk like a duck
this man swim like a duck


class Lame_Foot_Duck():
  def swim(self):
    print('i am lame but i can swim')

lame_duck = Lame_Foot_Duck()
watch_duck(lame_duck)

output >>
AttributeError: Lame_Foot_Duck instance has no attribute 'walk'
Copy after login

watch_duckThe function receives the object of this class, and then does not check the type of the object, but directly calls the sum of the object If the required method does not exist, an error will be reported.

The specific embodiment of duck type in python is as shown in the following code

class CollectionClass():
  lists = [1,2,3,4]
  def __getitem__(self, index):
    return self.lists[index]

iter_able_object = CollectionClass()

class Another_iterAbleClass():
  lists=[1,2,3,4]
  list_position = -1

  def __iter__(self):
    return self

  def next(self): #还有更简单的实现,使用生成器或迭代器什么的:)
    self.list_position += 1
    if self.list_position >3:
      raise StopIteration
    return self.lists[self.list_position]

another_iterable_object=Another_iterAbleClass()

print(iter_able_object[1])
print(iter_able_object[1:3])
output>>
2
[2, 3]

another_iterable_object[2]
output>>
Traceback (most recent call last):
 File "/Users/steinliber/a.py", line 32, in <module>
  another_iterable_object[2]
TypeError: &#39;Another_iterAbleClass&#39; object does not support indexing

print(next(another_iterable_object))
output>>
1
print(next(another_iterable_object))
output>>
2

print(next(iter_able_object))
output>>
Traceback (most recent call last):
 File "/Users/steinliber/a.py", line 29, in <module>
  print(next(iter_able_object))
TypeError: IterAbleClass object is not an iterator
Copy after login

In python, the implementation method of the above code is called protocol ( Protocol), these protocols can be regarded as notification interfaces, which specify which methods of the object the caller needs to call to use this function, and which methods the callee needs to implement to complete this function. The difference between it and the interface in Java is that the interface function in Java needs to be implemented through inheritance. The inherited class must implement all the abstract methods in the interface, so the concept of type is emphasized in Java, while protocol in python It is more informative. A function specifies which methods of the incoming object need to be called to implement a certain function. All classes that implement these methods can implement this function.

Specifically speaking from the above two classes, the first class implements the __getitem__ method, then the python interpreter will treat it as a collection, and the objects of this class can be Use slicing, get sub-items and other methods. If the second class implements the __iter__ and next methods, python will think it is an iterator, and you can use it on the object of this class. Loop through each child item. A class can implement methods that it is capable of implementing, and can only be used in situations where it makes sense.

Compared with the duck class above, these two classes are actually [] used for edge cutting (it actually calls python's slice function) and ## used for looping. #iter() is equivalent to the watch_duck function. These functions receive objects of any class and call methods in the objects required to implement the function to implement the function. If the method called in the function If the object does not exist, an error will be reported.

As can be seen from the above, the flexibility of python duck typing is that it focuses on how the called object is used, rather than what the object type itself is. Therefore, it is not recommended to use

isinstance in python to determine the type of incoming parameters. A more pythonic method is to directly use the incoming parameters through try, exceptTo handle the situation where the incoming parameters do not meet the requirements. We should use the object by passing in its capabilities rather than passing in the object's type.

Summary

The above is a detailed introduction to Python duck types. The content of this article is still very detailed. I hope it will be helpful to everyone learning python. , if you have any questions, you can leave a message to communicate.

For more detailed articles on Python’s duck type, please pay attention to the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

How Much Python Can You Learn in 2 Hours? How Much Python Can You Learn in 2 Hours? Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Exploring Its Primary Applications Python: Exploring Its Primary Applications Apr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

See all articles