From the python documentation, the classmethod is roughly equivalent to
class ClassMethod(object):
"Emulate PyClassMethod_Type() in Objects/funcobject.c"
def __init__(self, f):
self.f = f
def __get__(self, obj, klass=None):
print(klass)
if klass is None:
klass = type(obj)
def newfunc(*args):
return self.f(klass, *args)
return newfunc
My question is: In what cases, the klass is None.
I tested with a Test class
class Test(object):
def __init__(self):
pass
@ClassMethod
def fromstring(cls, s):
res = cls()
res.s = s
return res
t1 = Test.fromstring("a")
t2 = Test()
t3 = t2.fromstring("a")
In those cases, the klass is <class 'main.Test'>.
t2.fromstringis the same thing asTest.fromstring, so I'm not sure what you're trying to show with that