Home >Backend Development >Python Tutorial >Are there member variables in python?
There are two main types of variables used in python classes: class variables and member variables. Class variables are common to all instantiated objects of the class, while member variables are unique to each instantiated object.
#The following is explained through two small programs. (Recommended learning: Python video tutorial)
class A(object): def __init__(self): #aa为成员变量 self.aa = 10 @staticmethod def test(self): self.aa += -1 if __name__ == '__main__': x = A() y = A() #调用x x.test(x) print x.aa #输出9 y.test(y) print x.aa #输出9 print y.aa #输出9
We can obtain it in the destructor as self.aa, but obviously, aa at this time is in the form of a member variable appears, the modifications made to it at this time can only be directed to its object itself and will not affect other class objects. I think this design should be more consistent with the definition of a destructor, otherwise when an object exits the scope, it will be a particularly dangerous thing for other objects.
For more Python related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of Are there member variables in python?. For more information, please follow other related articles on the PHP Chinese website!