Skip to content
UPEEC

classmethod and staticmethod

classmethod receives the class (alternative constructors); staticmethod receives nothing extra.

Easy
Comments 0
class Resistor:
    def __init__(self, ohms): self.ohms = ohms
    @classmethod
    def from_code(cls, code: str):    # "472" -> 4700
        return cls(int(code[:2]) * 10 ** int(code[2]))
    @staticmethod
    def series(*rs): return sum(rs)

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.