Learning to polymorphic, not sure whether they fully understand the Python polymorphism, so Google watched a lot of information and literature, found that many programmers speak too abstract, not easy for beginners to understand, this violates the original intention of Python: simple. So I made a summary here in a popular way. First put a piece of code, in the Python3 example: (in order to give non-trained and middle school English partial section Python learners a more friendly reading experience, W3n deliberately added Chinese comments, can not look at the code, read through comments, the main purpose is logical understanding, code Secondary) class Human ( Object): # Create "People" Class def __init__ (self): print (' I can speak ') # I'll Talk. Class Man (Human): # Create "Men" Class def __init__ (self): & nbsp; print (' I has to prepare money for woman in usually # in China, usually a man must prepare a gift and a lot of money for a wife and a person. Class Woman (Human): Create a "Women" Class def __init__ (self): print (' I don't need the money from the If he Love me aha o Cute that believe it ') # if a Man loves me, I don't care if he spends money on me. Huh, huh? You believe that can only show that you are too naïve. Print (' Human: ') H = Human () print (' Man: ') m = Mans () print (' Woman: ') W = woman () # Create a variable h, m, Wprint, respectively, for humans, men, and women (ISinstance (H,human)) print (Isinstance (M,man)) print (Isinstance (M,human)) # Look in this, it's true, M is isn't only man B UT also human to determine whether H is human, M is a man, M is human, the output is all. #输出结果是: #human: #I can Speak#man: #I has to prepare money for woman in usually in China#woman: #I does not need the money from The man if he love me. Aha, you're so cute, this example of believe it#true#true#true m (variable, x like "x = 3") is not only the man (contrast int) category, but also the human (object) category, which is the polymorphism of the variable; In addition, when m calls the __init__ method, the output content is different from the human __init__, which is the polymorphism of the polymorphic function. In addition, for Python learners to better learn python,opw3n specifically here to inform, Python polymorphism in the domestic reference is still a different controversy, even if you do not understand the python more than the same, does not affect you continue to learn python, you can skip the " Python polymorphic ".For object-oriented polymorphic understanding, it is recommended to refer to duck type. To facilitate reading, the following content is introduced from Wikipedia:Duck Type:reference Duck type, duck type (English: Duck typing) is a style of dynamic type. In this style, an object's valid semantics are determined not by inheriting from a particular class or implementing a particular interface, but by the "collection of current methods and properties". The name of the concept derives from the duck test presented by James Whitcomb Riley, which can be described as "duck test":
-
"When you see a bird walking like a duck, swimming like a duck and barking like a duck, the bird can be called a duck." "
In duck type, the focus is not on the type of the object itself, but how it is used. For example, in a language that does not use duck type, we can write a function that takes an object of type "Duck" and invokes its "walk" and "call" methods. In a duck-type language, such a function can accept an arbitrary type of object and invoke its "walk" and "call" methods. If the method that needs to be called does not exist, a run-time error is raised. Any object with such a correct "walk" and "call" method can be accepted by the function, which leads to the above expression, and hence the name of this type of decision. Duck types usually benefit from the "No" test method and the type of parameters in the function, but rely on documentation, clear code, and testing to ensure proper use. If you have any mistake, please advise me.
The understanding of polymorphism in Python.