python everything is an object
All but a few statements in python are objects! Create a class, which is called a class object. It also occupies memory space and has its value.
class Student: pass print(id(Student)) #2926222013040 print(type(Student)) #<class 'type'> print(Student) #<class 'main_.student'>
Composition of classes
- Class attribute: variables defined outside methods in a class are called class attributes and are shared by all objects of the class
- Instance property: the property defined when initializing an instance object. It is not shared between different objects.
- Example method: equivalent to function, but the name has changed. What is defined outside the class is called a function, and what is defined inside the class is called a method.
- Static method: a method decorated with @ staticmethod and directly accessed by class name
- Class method: a method decorated with @ classmethod and accessed directly by class name
class Student: def __init__(self,name,age): #Languages such as C + + add a bracket after the class name student, which defines the parameters required for instantiation. python just moves it to__ init__ Here you come and assign values. #Instance properties self.name = name #self.name and self Age is called an instance attribute and assigns the value of the local variable name to the entity attribute. self.age = age native_place = 'Jilin' #Variables written directly in a class are called class attributes #Instance methods (functions defined outside the class and methods defined inside the class) def info(self): #self exists whether it is written or not print('Student information') @staticmethod #Static method def method(): #Cannot write self print('Static method') @classmethod #Class method def cm(cls): #cls stands for class print('Class method')
Note that some of the parentheses of different methods in the class do not use parameters, and some need self or cls. What does this mean?
- Self: the self keyword when defining an instance method in a python class is related to the meaning of the instance method. The instance method is called by the instantiation object of the class. The instantiation object calling it needs to be passed into the instance method. Self is the formal parameter representing the instantiation object. To put it bluntly, because a class can create multiple instance objects, you should know which object is calling the instance method. Otherwise, if you want to output Zhang San's information, you may run to Li Si.
- cls: similar to the understanding of the self keyword, the instance method is to distinguish which instance object calls the method and which class calls the method. Only when two classes have class methods with the same name can they be distinguished.
Class instantiation and use
Instantiate the class to obtain the instance object of the class object (in fact, the class object is to emphasize that the class in python is also an object). With an instance, you can call the content in the class
1. Class instantiation and use of methods and properties:
There are two methods for calling instance methods:
- Object name Method name
- Class name Method name (object name)
# __ init__ There are two parameters in initialization, so you need to pass in two parameters when instantiating an object stu1 = Student('Jack',20) #Call properties and methods with "object name. Method name" stu1.info() #Student information print(stu1.name) #20 #You can also call a method with "class name. Method name (object name)" - > in fact, it is self at the method definition Student.info(stu1) #Student information, function and stu1 Same as info()
2. Use of class properties, class methods, and static methods
- Class properties can be called by class objects or instance objects, but can only be modified by class objects.
In fact, when the instance object calls a class attribute, the instance object will copy the class attribute to become its own instance attribute. Therefore, if you try to modify this attribute in the instance object, you only modify an instance attribute of the object and will not affect the class attribute.
stu1 = Student('Zhang San',20) stu2 = Student('Li Si',22) print(stu1.native_place) #Jilin print(stu2.native_place) #Jilin #Modifying this property of the instance object does not affect the class property stu1.native_place = 'Tianjin' print(stu1.native_place) #Tianjin print(Student.native_place) #Jilin #Modifying this property of a class object is called modifying a class property Student.native_place = 'Guangdong' print(stu1.native_place) #Guangdong
- Class methods can be called through classes or instance objects.
Class method is used to simulate the case where java defines multiple constructors. Since there can only be one initialization method in a Python class, classes cannot be initialized according to different situations.
#There is no need to pass in parameters in parentheses, so it represents the class method of Student class cm Student.cm() #Class method
class Book(object): def __init__(self, title): self.title = title @classmethod def create(cls, title): book = cls(title=title) return book book1 = Book("python") book2 = Book.create("python and django") print(book1.title) #python print(book2.title) #python and django
- Static methods can be called through classes or instance objects.
When a method does not need to access any instance methods and properties (because static methods cannot access these), it is suitable to define it with static methods only by passing in parameters and returning data.
Its only function is not to use the self parameter. There is no real static method in python, because the class itself is an instance (the type instance inherited from the object and the bare classobj instance). Therefore, a "static method" is actually just a member of the class definition object.
The static method of c + + is a serious static method, because the class definition itself is not an instance.
Student.method() #Static method
Dynamic binding properties and methods
python is a dynamic language. After creating objects, you can dynamically bind properties and methods. That is, the instance object can be bound with properties or methods that the class to which the object belongs does not have.
class Student: def __init__(self,name,age): self.name = name self.age = age #Define a function outside the class def show(): print('This is an out of class method') #Initialize two instance objects stu1 = Student('Zhang San',20) stu2 = Student('Li Si',22) #Class Student does not have the attribute gender, but it can be customized for stu2 objects through dynamic binding. Since it is customized, other objects that are not bound must not have this attribute stu2.gender = 'male' print(stu2.name,stu2.age,stu2.gender) #Li Si 22 male print(stu1.name,stu1.age,stu1.gender) #An error is reported because stu1 does not bind the attribute gender stu1.show = show() stu1.show() #This is an out of class method stu2.show() #An error is reported because stu2 does not bind the attribute gender