https://docs.python.org/3/library/functions.html
The @classmethod decorator transforms a method into a class method. A class method receives the class as implicit first argument, just like an instance method receives the instance. Class methods are useful for creating alternate class constructors.
# Syntax.
class C:
@classmethod
def f(cls, arg1, arg2, ...):
statements
# Calling: C.f() or C().f() [the instance is ignored except for its class]
class Poly:
def __init__(self, c=0, n=0):
self.data = (n+1) * [0]
self.data[-1] = c
@classmethod
def fromiterable(cls, iterable):
new_poly = cls()
new_poly.data = list(iterable)
return new_poly
poly1 = Poly.fromiterable((3, 5, 7))
assert poly1.data == [3, 5, 7]
assert isinstance(poly1, Poly)
class BetterPoly(Poly): pass
poly2 = BetterPoly.fromiterable([4, 3, 2])
assert isinstance(poly2, BetterPoly)