I understand from the question "Difference between 'cls' and 'self' in Python classes?" that cls
is used in class methods, whereas self
is used in instance methods.
However, I used self
in class methods and it is still working fine.
For example,
class Test:
@classmethod
def hello(self, name):
print ('hello '+name)
Test.hello('Tom')
What, exactly, does cls
or self
do in class methods?
You can use any name you want. But as per the naming standard defined within PEP 8 (Style Guide for Python Code), it's better to name self
for the first argument to instance methods and cls
for the first argument to class methods.
Function and Method Arguments
Always use
self
for the first argument to instance methods.Always use
cls
for the first argument to class methods.