The most complete history of the Python object-oriented knowledge Reorganizing

Class: used to describe the same set of properties and methods of an object. It defines the set of properties and methods common to each object. Objects are instances of classes.
Class variables: Class variables are common throughout the instantiated object. And class variables defined outside the body of the function in the class. Class variables are typically not used as instance variables.
Data members: class variables or instance variables for processing data related to the class and object instance.
Method overrides: If you can not meet the needs of the subclass inherits from a parent class, can be rewritten to cover methods of this process is called, also known as the overriding method.
Examples of variables: variables defined in the process, only the role of the current instance of the class.
Also note: Whether you are for Python employment or hobbies, remember: project development experience will always be the core of a new project if you lack training or tutorial succinctly no python, Python can go small series of exchanges skirt: Seven Yiyi nine hundred seventy-seven bar and under five (digital homonym) conversion can be found, there are many new tutorial project, you can also communicate with older drivers for advice!
Inheritance: that is, a derived class (derived class) inherits the base class (base class) fields and methods. Inheritance also allows a derived class object as a base class object treated.
Create a class a

 variable is a variable, its value will be shared between all instances of this class. You can access the class used inside or outside the class P.
A first method __init __ () method is a special method, constructor, or class initialization method is called when an instance of that class is created which will call
instance self representative of the class, the class definition of the self when the method must be some, although not necessarily the appropriate parameters passed when calling.
Examples of representatives of the class of self, rather than the class
Method in class only one specific difference from ordinary functions - they must have an extra first parameter name, as is customary its name is self.
Input: Output: can clearly be seen from the results of the implementation, Self represents the instance of the class, representative of the address of the current object, and points to the class self.class. self is not a python keyword, we can also put him into runoob normally do: create an instance of Object Access attribute: You can use Point property to access the object (.). Name of the class using the following access class variables: the Python built class attribute __dict__: class attributes (including a dictionary, the data attributes of the class consisting of) the __doc__: Document string class __name__: class name __module__: where the class definition module (class full name is '__main __ className.', if the class is in an imported module mymod, then className .__ module__ equal mymod) __bases__: class all parent constituent element (contains a tuple of all the parent class consisting of) the Python built call class attribute examples are as follows: Python destroying objects (garbage collection) the Python using this simple reference counting technique for tracking and garbage. In the internal records of all Python objects use the respective numbers of references. An internal tracking variables, called a reference counter.




















When an object is created, it creates a reference count, when the object is no longer needed, that is to say, the object's reference count goes to zero, it is garbage collected. But the recovery is not "immediately" by the interpreter at the appropriate time, will take up memory space garbage objects recovered. Garbage collection mechanism not only for a reference count of the object 0, the same can also handle the case of circular references. Circular reference means that two objects refer to each other, but there is no other variable references them. In this case, only the reference count is not enough. Python's garbage collector is actually a reference to a cycle counter and garbage collector. As a supplementary reference counting garbage collector will pay attention to the total amount allocated large object (and not counting those destroyed by reference). In this case, the interpreter will pause, trying to clean up all the unreferenced cycle. Class inheritance One of the major benefits of object-oriented programming code reuse is brought to realize one of such reuse is through inheritance. Inheritance can fully understand the relationship between types and subtypes into classes. Caveats: class inheritance syntax derived class name (the base class name): the name of the base class written in brackets, when the base class is the class definition, in the tuple specified. Inheritance in python some of the features: 1: In the following structure (__init __ () method) Cheng Zhongji class will not be automatically invoked, it requires a special person called in the constructor of its derived class. 2: when the method is called a base class, the class name prefix need to add the base class, and the need to bring the self parameter variables. Do not need to bring self parameters different from the ordinary function calls in class 3: Python always first find the corresponding type of method, corresponding method if it can not find in a derived class, it began to look one by one to the base class. (First find method call in this class, the base class can not be found before going to look for). If the column more than one class in the inheritance tuple, then it is called "multiple inheritance." Input  :












Output: can inherit multiple classes: call method overrides input: output: class attributes and methods of









the private properties of the class
__private_attrs: two begins with an underscore, stating that the property is private and can not be used or accessed directly outside the class. Self .__ private_attrs method as used in the interior of the class.
Methods of the class
within the class, using the def keyword can be defined as a class method, with the general function to define different class method must include a parameter self, and is the first parameter
proprietary method of the class
__private_method: the first two underscore statement this method is a private method can not be called in class to the outside. Internal class calls self .__ private_methods
Input: Output:



single underline, the double underline, the double underline craniocaudal Description:
__foo__: Laid-column method is defined, similar __init __ () or the like.
_foo: a single underscore represents the beginning of the protected types of variables, namely the protection type can only allow it to itself and the subclass access, can not be used * Import Module from
__foo: double underline indicates the type of private (private) of variables can only be allowed to visit the class itself.
A final note: Whether you are for Python employment or hobbies, remember: project development experience will always be the core of a new project if you lack training or tutorial succinctly no python, Python can go small series of exchanges skirt: Seven Yiyi nine under seventy-seven bar and five (digital homonym) conversion can be found, there are many new tutorial project, you can also communicate with older drivers for advice!

Text and images in this article from the network with their own ideas, only to learn, exchange, not for any commercial purposes, belongs to original author, if any questions, please contact us for treatment.

Guess you like

Origin www.cnblogs.com/chengxuyuanaa/p/12576399.html