October 19, 2024

Use classmethod when you need to access class variables or when the behavior should change in subclasses.

Use staticmethod when the behavior should remain unchanged even in subclasses.

Class Definition

class American(Person):
    
    def __init__(self, age: int):
        self.age = age
    
    @classmethod
    def from_arguments(cls, age: int) -> American:
       return cls(age)

    @staticmethod
    def species():
        return 'human kind'

Usage

John = Person.from_arguments(33)
print(john.age, type(john)) # 33, Person