Class attributes & instance attributes
Class attribute
Class attributes are common to all objects, that is, all objects will use the same class attribute, which is defined inside the class.
The class attribute can be called directly by the class name. Modifying the class attribute will change when all objects are used.
class Student:
name = 'chiruno'#类属性
height = 1.56
obj1 = Student()# 实例对象
obj2 = Student()
print(obj1.height)# 通过实例调用类属性
print(obj2.height)
Student.height = 1.60 # 通过类名修改类属性
print(obj1.height)
print(obj2.height)
'''
输出:
1.56
1.56
1.6
1.6
'''
Instance attributes
Instance attributes are owned by objects, and each object will have different instance attributes.
Instance attributes need to be called through the instance, the same inside the class
class Student:
name = 'chiruno'
height = 1.56
def set_grade(self):
self.grade = 100# 类内部增加实例属性,self相当于java里的this
obj1 = Student()
obj2 = Student()
obj1.grade = 95# 增加实例属性
obj2.grade = 99
print(obj1.grade)
print(obj2.grade)
'''
输出:
95
99
'''
You can also assign uniform instance attributes to all objects through the __init__() method. The init method is equivalent to the construction method and will be called when the instance is initialized.
class Student:
name = 'chiruno'
height = 1.56
def __init__(self, grade: int):
self.grade = grade
The nature of both
Class attributes exist alone in a memory area, and all instances refer to them
Modifying the class attribute should be modified by calling the class name. If it is modified through the instance, the attribute of the instance will be added.
This is because when the instance is modified, it will be considered as a new attribute in the instance.
It also illustrates another problem. When the class attribute and the instance attribute have the same name, the call will give priority to the instance attribute.
class Student:
name = 'chiruno'
height = 1.56
obj1 = Student()
obj1.height = 111
print(Student.height)
print(obj1.height)
'''
输出:
1.56
111
'''
But if it looks like a variable type, it can be modified by an instance, but it will still be considered to create an instance attribute when using =
class Student:
name = 'chiruno'
friends = []
obj1 = Student()
obj2 = Student()
obj1.friends.append('sakuya')
Class method
To define a class method, you need to add a decorator to the ordinary method:
@classmethod
and then this method will become a class method. The class method exists in the memory of the class, and all its instances will get a reference.
You can modify and view the class through the class method. Attributes
class Student:
name = 'chiruno'
friends = []
@classmethod
def set_name(cls, name):
cls.name = name
obj1 = Student()
print(id(obj1.set_name))
print(id(Student.set_name))
'''
输出:
2834049612872
2834049612872
'''
Static method
Similar to a class method, add a decorator to the method :
@staticmethod
and the method will become a static method. The
static method can also modify the class attribute by the class name
class Student:
name = 'chiruno'
friends = []
@classmethod
def set_name(cls, name):
cls.name = name
@staticmethod
def add_friend(frd):
Student.friends.append(frd)
Through the dir function, you can easily find all the attributes in a certain area
class Student:
name = 'chiruno'
friends = []
@classmethod
def set_name(cls, name):
cls.name = name
@staticmethod
def add_friend(frd):
Student.friends.append(frd)
print(dir(Student))