Class and static methods
A normal method receives the instance as self. Two decorators change what a method receives.
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
@classmethod
def from_text(cls, text):
x, y = text.split(",")
return cls(int(x), int(y))
@staticmethod
def is_valid(text):
return text.count(",") == 1
class Pixel(Point):
pass
p = Pixel.from_text("3,4")
print(type(p).__name__, p.x, p.y)
print(Point.is_valid("1,2"))classmethod
The method receives the class as cls, whether it is called on the class or on an instance. Its main use is alternative constructors such as from_text, from_dict or the built-in dict.fromkeys. Calling cls(...) instead of Point(...) means a subclass gets an instance of itself, as Pixel does here.
staticmethod
The method receives nothing extra. It is a plain function kept in the class because it belongs there, and it can be called on the class or on an instance. If it has nothing to do with the class, a module-level function is just as good.
Your task
Write Color(r, g, b) that limits each channel to 0..255 with a static method clamp(value), a class method from_hex(text) turning "#ff8000" into the matching color of whichever class it is called on, and a repr like Color(255, 128, 0) that uses the instance's own class name.
The explanation opens here once every check passes.