Having a class
class A(object):
z = 0
def Func1(self):
return self.z
def Func2(self):
return A.z
Both methods (Func1 and Func2) give the same result and are only included in this artificial example to illustrate the two possible methods of how to address z.
The result of Func* would only differ if an instance would shadow z with something like self.z = None.
What is the proper python way to access the class variable z using the syntax of Func1 or Func2?
I would say that the proper way to get access to the variable is simply:
No need for
Func1andFunc2here.As a side note, if you must write
Func2, it seems like aclassmethodmight be appropriate:As a final note, which version you use within methods (
self.zvs.A.zvs.cls.zwithclassmethod) really depends on how you want your API to behave. Do you want the user to be able to shadowA.zby setting an instance attributez? If so, then useself.z. If you don’t want that shadowing, you can useA.z. Does the method needself? If not, then it’s probably a classmethod, etc.