pythonIntroduction of object oriented technology
Class variable: class variable is common in the whole instantiated object. Class variables are defined in the class and outside the function body. Class variables are usually not used as instance variables.
Data member: a class variable or instance variable is used to process data related to a class and its instance objects.
Method Rewriting: if the method inherited from the parent class cannot meet the needs of the child class, it can be rewritten. This process is called method overwriting, also known as method rewriting.
Instance variable: the variable defined in the method, which only affects the class of the current instance.
Also note: whether you are for Python employment or hobbies, remember: project development experience is always the core. If you lack new project practice or Python intensive tutorial, you can go to a small Python exchange : you can find it under the conversion of “seven clothes, nine seven buses and five numbers”. There are many new tutorial projects in it. You can also exchange and consult with the old driver!
Inheritance: a derived class inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object.
aVariable is a class variable whose value will be shared among all instances of this class. You can use p access in internal or external classes.
The first method__ init__ () method is a special method called the constructor or initialization method of a class, which is called when an instance of the class is created
Self represents an instance of a class. It is necessary for self to define a method of a class, although it is not necessary to pass in corresponding parameters when calling.
Self represents an instance of a class, not a class
There is only one special difference between a class’s methods and a normal function – they must have an extra first parameter name, which by convention is self.
From the execution result, it is obvious that self represents the instance of the class, represents the address of the current object, and self.class Point to the class.
Self is not a python keyword. We can replace it with runoob to execute normally:
Create instance object
Access properties: you can use points (.) to access the properties of an object. Use the name of the following class to access class variables:
Python built in class properties
Wei dict__ : properties of a class (contains a dictionary, which consists of data properties of the class)
Wei doc__ : document string for class
Wei name__ : class name
Wei module__ : the module where the class definition is located (the full name of the class is’__ main__ . classname ‘, if the class is in an import module mymod, then classname__ module__ Equal to mymod)
Wei bases__ : all parent elements of a class (including a tuple of all parents)
The python built-in class property call example is as follows:
Python object destruction (garbage collection)
Python uses the simple technique of reference counting to track and recycle garbage.
Inside python, it records how many references each object in use has.
An internal trace variable, called a reference counter.
When an object is created, a reference count is created. When the object is no longer needed, that is, when the reference count of the object changes to 0, it is garbage collected. But recycling is not “immediate”. The interpreter will recycle the memory space occupied by the garbage object at the right time.
The garbage collection mechanism can not only deal with objects with a reference count of 0, but also deal with circular references. Circular references mean that two objects refer to each other, but no other variables refer to them. In this case, using only reference counts is not enough. Python’s garbage collector is actually a reference counter and a circular garbage collector. In addition to the reference count, the garbage collector also pays attention to objects that are allocated a large amount (and those that are not destroyed by the reference count). In this case, the interpreter pauses trying to clean up all unreferenced loops.
One of the main benefits of object-oriented programming is code reuse. One of the ways to achieve this reuse is through inheritance mechanism. Inheritance can be understood as the type and subtype relationship between classes.
Note: inheritance syntax class derived class name (base class name): the base class name is written in parentheses, and the base class is specified in the tuple when the class is defined.
Some features of inheritance in Python:
1: The construction of base class in inheritance (__ init__ () method) will not be called automatically. It needs to be called specifically in the construction of its derived class.
2: When calling the method of the base class, you need to prefix the class name of the base class and bring the self parameter variable. Unlike calling ordinary functions in classes, you don’t need to bring self parameters.
3: Python always finds methods of the corresponding type first. If it can’t find the corresponding methods in the derived classes, it starts to find them one by one in the base classes. (first find the called method in this class, and then find it in the base class.).
If more than one class is listed in the inheritance tuple, it is called “multiple inheritance”.
You can also inherit multiple classes:
Call method override
Class properties and methods
Private property of class
Wei private_ Attrs: starts with two underscores, declaring that the property is private and cannot be used or directly accessed outside the class. When used in methods within a class, self__ private_ attrs。
Method of class
Inside a class, you can define a method for the class by using the def keyword. Unlike the general function definition, the class method must contain the parameter self, which is the first parameter
Private method of class
Wei private_ Method: it starts with two underscores and declares that the method is private and cannot be called outside the class ground. Call self inside the class__ private_ methods
Description of single underline, double underline, head and tail double underline:
Wei foo__ : defines a specific method, similar to__ init__ () and so on.
A kind of Foo: a variable of protected type is indicated by a single underscore, that is, the protected type can only be accessed by itself and its subclass, and cannot be used for from module import*
Wei Foo: Double underscores represent variables of private type, which can only be accessed by the class itself.
Finally note: no matter you are for Python employment or hobbies, remember: project development experience is always the core. If you lack new project practice or Python intensive tutorial, you can go to small-scale Python exchange. Skirt: you can find it under the transformation of seven clothes, nine seven clothes, seven seven bars and five (Digital homophony). Many new tutorial projects can also be exchanged with old drivers for advice!
The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.