Let's suppose we have this class:
class Demo:
def __init__(self, str):
self.str = str
def fromBytes1(bytes):
return Demo(bytes2str(bytes))
@classmethod
def fromBytes2(cls, bytes):
return cls(bytes2str(bytes))
What is the difference between fromBytes1 and fromBytes2, except for the fact that you can't call the 1st method in the following way?
Demo().fromBytes1(bytes)
Is there something more subtle that I cannot see here?
selfparameter in the first case. Also, could you give the methods slightly different names?Demo.fromBytes(…)works), butDemo().fromBytes()does something unexpected.self.classmethodwill implicitly get passed the class as it's first positional argument@staticmethod.