Object-oriented programming in Python

Source: Internet
Author: User

In Python, you can almost complete all object-oriented programming elements in C + +.

Inheritance: Python supports multiple inheritance:

Class Derived (Base1, Base2, BASE3):

Pass

Polymorphic: All instance methods in Python are of type virtual

Packaging:

This is very special, C + + programming has been emphasized more is: in order to hide the implementation, all member variables are private type, and those that are only related to the implementation, not as an external interface methods are defined as private type (or protected). But in Python, there is no real private type, according to the Google Python style guide, for simple member variable access, you should directly pass the member's way without deliberately defining an access function. Also, the way to implement private is through code obfuscation. And look at the following code:

Class T (object):
def __init__ (self):
self.__x = 1.0
def __func (self):
Print ("Test")
t = t ()
T._t__func ()
Print (t._t__x)

Since the name of the member is preceded by two underscores (which require at least two), the resulting member name is confused and cannot be accessed by the original name. But still accessible, all private access controls inside Python

It is more a convention than a direct segregation of language features.

Finally, the understanding of the class in Python feels more like a namespace (which can be understood as a hash table that corresponds to a variable ID), and the name can be added dynamically (such as adding a member variable to an instance)

Object-oriented programming 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.