Class attributes are accessible through the class:
YourClass.clsattribute
or through the instance (if the instance has not overwritten the class attribute):
instance.clsattribute
Methods, as stated by ecatmur in his answer, are descriptors and are set as class attributes.
If you access a method through the instance, then the instance is passed as the self
parameter to the descriptor.
If you want to call a method from the class, then you must explicitly pass an instance as the first argument. So these are equivalent:
instance.method()
MyClass.method(instance)
Using the same name for an instance attribute and a method will make the method hidden via the instance, but the method is still available via the class:
#python3
>>> class C:
... def __init__(self):
... self.a = 1
... def a(self):
... print('hello')
...
>>> C.a
<function a at 0x7f2c46ce3c88>
>>> instance = C()
>>> instance.a
1
>>> C.a(instance)
hello
Conclusion: do not give the same name to instance attributes and methods.
I avoid this by giving meaningful names. Methods are actions, so I usually use verbs or sentences for them. Attributes are data, so I use nouns/adjectives for them, and this avoids using the same names for both methods and attributes.
Note that you simply cannot have a class attribute with the same name as a method, because the method would completely override it (in the end, methods are just class attributes that are callable and that automatically receive an instance of the class as first attribute).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…