"Static" member variables in python classes

In Python, define a variable StaticVar in the Static class, and initialize it to be equal to 5, a, b, and c as the three objects of the class;

After a adds one to its own attribute StaticVar, it breaks away from the attribute of the class and becomes a's own attribute. Therefore, when the attribute StaticVar of the class increases by 2, it will not affect a, but because b and c Neither has made changes to their own properties, StaticVar, so they are not separated from the properties of the class, so when the properties of the class increase by 2, the properties of b and c are also increased by 2.

class Static():
	StaticVar=5
	def varfunc(self):
		self.StaticVar += 1
		print (self.StaticVar)

print(Static.StaticVar)
a=Static()
a.StaticVar+=1
b=Static()
c=Static()
Static.StaticVar+=2
print(a.StaticVar)
print(b.StaticVar)
print(c.StaticVar)

Guess you like

Origin blog.csdn.net/wxy_csdn_world/article/details/80756910