Python advanced features for object-oriented programming

Source: Internet
Author: User

Here is still based on the online information, mainly Liao Xuefeng Teacher's tutorial study notes, the main introduction of Python object-oriented advanced features, notes are not all, just record oneself feel error prone places * * *

1.python as a dynamic language, his dynamic binding mechanism allows to dynamically add methods and properties to class or object instances during the run, which is difficult to do in a static language such as Java;

1) Dynamic binding properties:

      

    

2) Dynamic binding method

The method of binding to an instance is not visible to other instances and classes: ( This also shows that the dynamic binding method for an instance must be Methodtype (func, instance))

    

But the methods that are bound to the class are visible to all instances

    

2.__slots__ = (' attribute1, Attribute2 ') in order to restrict binding property names to classes, but not for inherited subclasses:

    

There is no restriction on inherited subclasses:

    

As long as there is a __slots__ = () statement inside the class, only the object of the class is bound to the property in parentheses and the allowed binding property inherited from the parent class, (there are two types, the parent has slots statements and no slots statements):

    The parent class does not have a slots statement, and although the subclass has restrictions, it can still bind other types:

    

The parent class has the slots statement above already has, does not demonstrate;

3. Multiple Inheritance:

1) Java only allows single re-inheritance, Pyhon allows multiple inheritance, with mixin design implementation;

2) The advantage of mixin is to add multiple functions to the class, have good extensibility, write the function as Xxxmixin class, and inherit the property and method directly.

3) Use mixin not to design multi-layered complex inheritance relations;

Python advanced features for object-oriented programming

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.