Python Class and object parsing

Source: Internet
Author: User
Tags class definition

Class Members:
# field
-Normal field, save in object, execute only through object access
-static fields, which are saved in the class, can be accessed through the object or accessed through the class

# method
-Normal method, saved in class, called by object, self= "Object
-static methods, which are saved in the class, are called directly by the class, and objects can also be called
-Class method, saved in class, called directly by class, cls= "Current class, object can also call

classfoo:nation=' China'#static fields    def __init__(self, name): Self.name= Name#normal field        defShow (self):#Common Methods        Print(self.name) @staticmethod#Static Methods    defstat (A1,A2):Print(A1,A2) @classmethod#class Method    defCLASSMD (CLS):#CLS is the class name        Print(CLS)Print('CLASSMD') obj= Foo ('PiS') Obj.nameobj.show ()
#Foo. Show (obj) #也能成功调用, passed in the object parameter Foo.stat (The) #类直接调用Foo. CLASSMD () #类直接调用
Obj.stat (#对象直接调用)
OBJ.CLASSMD () #对象直接调用

Python does not have a real private property (C + + has public protect private)

In fact: the properties defined with __ are only gaiminghuanxing.

The meaning of the attribute defined by _ is to evoke the user's attention as a private attribute. It's actually just a common method.

Representation: A double-underlined function or property that can be called and accessed in a class definition (a method in a class), an instance of the class cannot be accessed directly, and the subclass is inaccessible.

A single underlined function or attribute, in a class definition (a method in a class) can be called and accessed, an instance of the class can be accessed directly, and the subclass can be accessed;

For a double-underlined function or property, the Python interpreter uses the method of name obfuscation, turning the private method "__method" into "_classname__method".

classBase (object): #object as the base class, there are some common methodsdef __private(self):Print("private value in Base")    def_base__private (self):Print("_base__private value in Base")#If there is no definition, the previous method is called    def_protected (self): #已被覆盖Print("protected value in Base")    defPublic (self):Print("Public value in Base") self.__private()"""the method of calling the parent class is-----------for the following reasons: Self.__private () method call first from the subclass, and then from the parent class to find, no error.    If it is a generic method call of a subclass, although the private method of the subclass has been renamed, it can be recognized, and the private method of the subclass is called.    The public invocation of the parent class is now, and the private method of the subclass has been renamed and is not recognized.    The public method of the parent class can only find and invoke the __private () method of the parent class. We know that the generic method of the subclass can invoke the private method of the subclass (although it has been renamed), as is the parent class.    So you can find the private method of the parent class. So the private method of the parent class is renamed? We added code to test------has changed"""self._protected ()#has been overwrittenclassDerived (Base):def __private(self):Print("Override Private")    def_protected (self):Print("Override protected") d= Derived ()#Creating ObjectsD.public ()#methods that inherit from the parent classD._protected ()#methods for calling subclassesD._derived__private ()#calling Subclass __private () is not recommended for use. Does not accord with the original intention. #d.__private () #已改名, cannot be called. The parent class does not even see the same way. 

The results are as follows:

in Baseoverride protectedoverride protectedoverride private  

Python inheritance

1. What is the method lookup order for multiple inheritance?

Class has a property named __mro__, whose value is a tuple that lists each superclass in the order of method parsing, from the current class up to the object class. The __mro__ attribute of class D is as follows:

(<class ' __main__. D ';, <class ' __main__. B ';, <class ' __main__. C ';, <class ' __main__. A ';, <class ' object ' >)

The popular point is the three main principles: a. Left priority
B. A road goes to the black
C. At the same root, the root is finally executed

2. constructor __init__ in single and multi-inheritance

single-Inheritance cases

(1) When the subclass defaults to No __init__, the parent class is inherited directly __init__

(2) When a subclass contains __init__, the parent class __init__ is not called automatically, and if you want to use a variable in the parent class __init__, you need to explicitly invoke the in subclass __init__

Multiple Inheritance Cases
(1) When a subclass inherits from more than one parent class, and the subclass is __init__, the Inheritance Order (MRO), which parent class is at the front and has its own __init__, inherits it; if the first parent class has no __init__, inherit the __init__ of the second parent class, if not __ init__, it is searched sequentially until one of the inherited parent classes contains __init__.

(2) subclasses inherit from more than one parent class, and subclasses with __init__, like single-Inheritance (2), do not automatically call all parent class __init__, if you want to use a variable in a parent class __init__, you need to explicitly call in the subclass __init__

Python Class and object parsing

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.