Basic Explanation and Application of python Class
- 2021-12-12 08:53:45
- OfStack
Definition of class
# class Is the keyword that defines the class, ClassName Is the name of the class
class ClassName:
# Write something else here
pass
class ClassName(object):
# Write something else here
pass
This is the simplest class definition. After python3, class definition 1 generally inherits object class, but it doesn't have much problem if it doesn't inherit.
Class object
Class objects are also called instances
# This is the creation of the 1 Class objects (instances), a Yes 1 Instances are also 1 Objects
a = ClassName()
Variable
There are: class variables, local variables, instance variables inside the class
Class variable
Class variables: All instantiated objects of a class share class variables at the same time, that is, class variables exist as common resources among all instantiated objects.
class ClassName(object):
# Definition 1 Individual class variable
name = "Tom"
There are two ways to call a class method, either directly using the class name or using the instantiation (object) of the class. The value of a class variable can be modified by the class name, and after modification, all instantiated objects will be affected.
# Called directly by class name
print(ClassName.name)
# Create an instance of a class (object)
a = ClassName()
b = ClassName()
// Called through an instance of the class
print(a.name)
print(b.name)
ClassName.name = "Tim"
print(a.name)
print(b.name)
The running result is:
Tom
Tom
Tom
Tim
Tim
However, if the value of the class variable of an instance is modified, the value of the variable is modified by the class name, which will not affect the class variable of this instance.
a = ClassName()
b = ClassName()
a.name = "asf"
ClassName.name = "Pig"
print(a.name)
print(b.name)
ClassName.name = "aaa"
print(a.name)
print(b.name)
Run results:
asf
Pig
asf
aaa
Through the running results, it can be seen that after the object a changes the value of name, it will call
Class Name.name = "Pig"
The name variable value of the object a does not change
Local variable
Local variables: Local variables are only defined within class methods and can only be used within methods
class ClassName(object):
# This is 1 Methods
def function(self):
# This is 1 Local variables
name = "Tom"
name is defined within the class method, so it is a local variable, which cannot be called externally and can only be used within the method.
Instance variable
Instance variable: A variable defined as an self. variable name, called an instance variable (attribute)
class ClassName(object):
# This is 1 Instance variables
def function1(self):
self.name = "Tom"
def function2(self):
print(self.name)
It only works on the object that calls the method, and can be called within other methods.
className = ClassName()
className.function()
Run results:
Tom
If defined in the
__init__()
The instance variable in the method can be accessed only by the object name.
class ClassName(object):
def __init__():
self.name = "Tom"
className = ClassName()
print(className.name)
Run results:
Tom
Private variable
__name
This is the private variable. Add two before the variable
_
,
self.__name
It is also a private variable, which cannot be accessed directly outside the class.
class ClassName(object):
# Defines the 1 Private class variables
__name = "Tom"
ClassName.__name
Run results:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
< ipython-input-8-08830fea0534 > in < module >
2 __name = "Tom"
3
---- > 4 ClassName.__nameAttributeError: type object 'ClassName' has no attribute '__name'
Methods of the class
Use in the class
def
Keyword to define methods, also called functions. The difference with the ordinary method is that you must need 1
self
Parameters of,
self
Represents the class itself
# This is the creation of the 1 Class objects (instances), a Yes 1 Instances are also 1 Objects
a = ClassName()
0
def
To define the keywords of the function,
function
Is the name of the function,
Special method
1.
__init__()
Parameter 1 used to initialize a class is generally called a constructor
This method is used by all classes.
# This is the creation of the 1 Class objects (instances), a Yes 1 Instances are also 1 Objects
a = ClassName()
1
When the class is instantiated, it is automatically called
__init__
2.
__call__()
Called when the object is treated as a function.
# This is the creation of the 1 Class objects (instances), a Yes 1 Instances are also 1 Objects
a = ClassName()
2
3.
__get__()
Class is called automatically when it is a property of another class
class ClassName:
def __init__(self):
pass
def __get__(self,instance, owner):
print("__get__ Called ")
class ClassName2:
a = ClassName()
Run results:
__get__ calls the
4.
__new__()
Is a static method responsible for creating class instances, and this method takes precedence
__init__()
The initialization method is called.
. . . There are many other class-specific methods
Inheritance
Inheritance is to obtain the property of parents in life, and to obtain all variables and methods of parents in programming languages. Deriving a new class from a class by inheritance
Single inheritance
In short, it inherits 1 class, and the inherited class is the base class (parent class) and the successor is a subclass
# This is the creation of the 1 Class objects (instances), a Yes 1 Instances are also 1 Objects
a = ClassName()
4
The above is a simple inheritance, and the subclass writes the name of the class to inherit in parentheses after the class name.
# This is the creation of the 1 Class objects (instances), a Yes 1 Instances are also 1 Objects
a = ClassName()
5
Run results:
Tom
..
It is found that the B class does not write any variables and methods, but calls the variables (properties) and methods of the A class
Of course, if you don't want a variable or method of the parent class, you can override this variable or method
# This is the creation of the 1 Class objects (instances), a Yes 1 Instances are also 1 Objects
a = ClassName()
6
Run results:
Tom
bbb
Multiple inheritance
In terms of name, multiple inheritance inherits multiple classes
# This is the creation of the 1 Class objects (instances), a Yes 1 Instances are also 1 Objects
a = ClassName()
7
The above C class inherits both A and B classes