Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Duck types of python Polymorphic Protocol

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "python polymorphic protocol duck type analysis". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Many different implementations of interfaces (protocols in python) are called polymorphisms. The role of polymorphism is to ensure the correct invocation of a property of an instance of any class in the family tree when inheriting and deriving.

From abc import ABCMeta, abstractmethod# duck class Dock (metaclass=ABCMeta): @ abstractmethod def Swimming (self): # Swimming method protocol (interface) pass @ abstractmethod# walking protocol (interface) def Walk (self): pass @ classmethod def _ _ subclasshook__ (cls, C): # determine whether another comparison class implements Swimming Walk protocol If these two protocols of duck class are implemented # then the type of the comparison class is a duck type # when the code executes to the object and this class makes isinstance type judgment, it will go to this function to judge the for method in ('Swimming' 'Walk'): for B in C. dogs class Dog: if method in B. dogs dogs class Dog: if B. dogs dogs class Dog (object) ): # implement swimming protocol def Swimming (self): print ("dog can plane") # implement walk protocol def Walk (self): print ("dog can walk") def Eat (self): print ("dogs like to eat bones") # turtle class Tortoise (object): # implement swimming protocol def Swimming (self): print (" Turtles can dive) # implement walk protocol def Walk (self): print ("turtles can walk") def Eat (self): print ("turtles like fish") dog = Dog () tortoise = Tortoise () print (isinstance (dog) Dock)) # Trueprint (isinstance (tortoise, Dock)) # True

As you can see, in the above code, as long as the swimming and Walk methods in the Dock class are implemented, then this class can be called the Dock class

Application scenarios such as: for loop, in python for loop can only be used for iterable objects, then my own defined class implements the _ _ iter__ protocol (interface), and this instance class is an iterable object that can be recycled by for.

The protocol class defined in python uses the @ abstractmethod decorator. The class decorated by @ abstractmethod cannot be initialized, as opposed to the pure virtual function class in C++.

This class can only be treated as a protocol (interface) class.

This is the end of the content of "python polymorphic protocol duck type analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report