CKTIS / Python
classmethod()
Binds the class as the first argument when a method is accessed through a class or instance.
Version: Python 3.14; repository image, production runtime not attested
Syntax
@classmethod
def make(cls):
return cls()Binds the class as the first argument when a method is accessed through a class or instance.
Calling an inherited factory on a subclass passes the subclass, allowing the factory to return a subclass instance.
The descriptor supplies the class as the implicit first argument when accessed through that class or one of its instances. Shape.make is inherited, but Square.make binds cls to Square. Calling cls() constructs a Square, so the type name printed by the program is Square.
class Shape:
@classmethod
def make(cls): return cls()
class Square(Shape): pass
assert type(Square.make()) is Square and type(Square().make()) is Square
Examples
class Shape:
@classmethod
def make(cls):
return cls()
class Square(Shape):
pass
print(type(Square.make()).__name__)
Result
Square
The descriptor supplies the class as the implicit first argument when accessed through that class or one of its instances. Shape.make is inherited, but Square.make binds cls to Square. Calling cls() constructs a Square, so the type name printed by the program is Square.