Duck Type in Python

Source: Internet
Author: User

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 implement a particular interface, but rather by the collection of current methods and properties.

"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 my personal understanding, duck type is a syntactic sugar, it does not emphasize the type of object, or where it inherits from, only care whether it can achieve the method I need, in general, regardless of your origin, regardless of the black Cat White cat, can catch the mouse is a good cat.

This dynamic type can be very convenient to achieve polymorphism,

Do not say, any only speak the vernacular does not take the code is bullying:

1 classPerson :2 3     defGo (self):4         Print("It is walking")5 6 classBird:7     defGo (self):8         Print("It is walking")9 Ten deffunc (a): One A.go () A  forIinch[Person (), Bird ()]: -Func (i)

Results:

IS wasin 0.2s] 

So?

Duck Type in Python

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.