Python Duck type

Source: Internet
Author: User

Once wrote an article about the polymorphism in Python, and finally concluded that Python does not support polymorphism, and as the understanding of Python deepened, there are some views on Python polymorphism.

First Python does not support polymorphism, nor does it support polymorphism, Python is a polymorphic language, advocating duck type. The following is a description of the types of ducks in Wikipedia:

In program design, Duck type (English: Duck typing) is a style of dynamic type. In this style, an object's valid semantics are not inherited from a particular class or implemented by a particular interface, but rather 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 method of walking and calling. In a duck-type language, such a function can accept an arbitrary type of object and invoke its method of walking and calling. If the method that needs to be called does not exist, a run-time error is raised. Any object that has such a correct way of walking and calling can be accepted by a function which leads to the above expression, and hence the name of this type of decision.
Duck types typically benefit from not testing the types of parameters in methods and functions, but rely on documentation, clear code, and testing to ensure proper use. Users who move from a static type language to a dynamic type language often try to add some static (before running) type checking, which affects the benefits and scalability of duck types and constrains the dynamic nature of the language.

-------------------------------------------

There is no doubt that the object in Python is also a piece of memory, in addition to the properties, methods in memory, but also contains the object type, we use reference to access the object, such as A=a (), first Python to create an object A, then declare a variable a, and then the variable A and object A are linked together. The variable A is not of type, its type depends on its associated object. A=a (), A is a reference to a type, we can say that a is a type, if you assign a value 3,a=3, at this point A is an integer reference, but Python is not a weak type language, in Python ' 2 ' +3 will error, and in PHP ' 2 ' +3 will be 5. As you can see, the variables in Python are similar to those in C, unlike C, where variables in Python can point to any type, although that's not very accurate, but easier to understand.

As a result, the parameters are not known until the parameter is passed through the Python runtime, although the methods in Python are late-bound, but the late binding to polymorphic in Java is different, and late binding in Java knows at least the type of the object. The type of the parameter is not known in Python.

Also cite the last example:

[Python]View PlainCopy
  1. Class A:
  2. def prt (self):
  3. print "A"
  4. Class B (A):
  5. def prt (self):
  6. print "B"
  7. Class C (A):
  8. def prt (self):
  9. print "C"
  10. Class D (A):
  11. Pass
  12. Class E:
  13. def prt (self):
  14. print "E"
  15. Class F:
  16. Pass
  17. def test (ARG):
  18. ARG.PRT ()
  19. A = A ()
  20. b = B ()
  21. c = C ()
  22. D = d ()
  23. E = e ()
  24. f = f ()
  25. Test (a)
  26. Test (b)
  27. Test (c)
  28. Test (d)
  29. Test (E)
  30. Test (f)

Output Result:

[Python]View PlainCopy
  1. A
  2. B
  3. C
  4. A
  5. E
  6. Traceback (most recent):
  7. File "/users/shikefu678/documents/aptana Studio 3 workspace/demo/demo.py", line -in <module>
  8. Test (a), test (b), Test (c), Test (d), Test (e), Test (f)
  9. File "/users/shikefu678/documents/aptana Studio 3 workspace/demo/demo.py", line , in test
  10. ARG.PRT ()
  11. Attributeerror:f instance has no attribute ' PRT '


A,b,c,d is a variable of type A, so you can get the desired effect (from the Java perspective), E is not a variable of type a but depending on the type of duck, it's like a duck, it's like a duck, it's like a duck, so this bird can be called a duck, E has a PRT method, So in the test method, E is a variable of type A, and F has no PRT method, so f is not a variable of type A.

The above is analyzed from the Java point of view, in fact, the top is a nonsense, just to illustrate the python running method. No one specifies what type of parameter the test method is to receive. The test method only specifies that you receive a parameter that invokes the PRT method of this parameter. At run time if this parameter has PRT method, Python executes, if not, Python error, because ABCDE all have PRT method, and F not, so get the upper result, this is how Python runs.

From learning python for 3 months, although not how to learn Java before, but the Java side of the book to see a lot of ways of thinking have changed, always want to use the Java way of thinking to think about the Python problem, in fact, it will only the opposite, There are too many things in python that are not the same as Java, from inside to outside.

Reprinted from: http://blog.csdn.net/shangzhihaohao/article/details/7065675

Python Duck type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.