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

How to understand Python Polymorphism

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to understand Python polymorphism". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand Python polymorphism".

Polymorphism literally means a variety of forms, such as black, yellow, white and so on. This is the different form of one kind of thing. In our python object-oriented, different objects will have different behaviors when they receive the same method or function. That is to say, each object can respond to the common function in its own way and achieve different results in different ways.

Polymorphism 1. Polymorphism

Polymorphism refers to a class of things that have multiple forms.

Animals come in many forms: people, dogs, pigs

Import abcclass Animal (metaclass=abc.ABCMeta): # same thing: animal @ abc.abstractmethod def talk (self): passclass People (Animal): # one of animal forms: human def talk (self): print ('say hello') class Dog (Animal): # Animal form II: dog def talk (self): print (' say wangwang') class Pig (Animal): # Animal form State 3: pig def talk (self): print ('say aoao')

Files come in many forms: text files, executable files

Import abcclass File (metaclass=abc.ABCMeta): # same thing: file @ abc.abstractmethod def click (self): passclass Text (File): # one of the forms of a file: text file def click (self): print ('open file') class ExeFile (File): # shape of a file II: executable file def click (self): print (' execute file') 2, polymorphism

What is polymorphic dynamic binding (sometimes called polymorphism when used in the context of inheritance)

Polymorphism refers to the use of instances regardless of the type of instance

In object-oriented methods, polymorphism is generally expressed as follows:

Send the same message to different objects (!! Obj.func (): is the method func that calls obj, also known as sending a message to obj func), different objects will produce different behavior when receiving (that is, methods).

That is, each object can respond to a common message in its own way. The so-called message is to call functions, and different behaviors refer to different implementations, that is, the execution of different functions.

For example: teacher. The bell rang, student. The bell rang (), the teacher performed the off-duty operation and the students performed the out-of-school operation, although the two messages were the same. But the effect of execution is different: peo=People () dog=Dog () pig=Pig () # peo, dog, pig are all animals, as long as they are animals, there must be a talk method # so we can directly use peo.talk () dog.talk () pig.talk () # instead of considering the specific type of them, we can define a unified interface to use def func (obj): obj.talk () duck type.

Teasing moment:

Python advocates the duck type, that is, "if it looks like, sounds like, and walks like a duck, then it is a duck."

Python programmers usually write programs based on this behavior. For example, if you want to write a custom version of an existing object, you can inherit the object

You can also create an entirely new object that looks and behaves like, but has nothing to do with it, which is often used to preserve the loose coupling of program components.

Example 1: using various' file-like 'objects defined in the standard library, although these objects work like files, they have no way to inherit built-in file objects

Example 2: sequence types have a variety of forms: strings, lists, tuples, but they directly have no direct inheritance relationship.

# both are like ducks, and both look like files, so you can use class TxtFile: def read (self): pass def write (self): passclass DiskFile: def read (self): pass def write (self): pass as files. Thank you for reading. That's what Python Polymorphism is all about. After the study of this article I believe you have a deeper understanding of how to understand Python polymorphism, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report