OOP - classmethod - staticmethod
examples/decorators/myclass.py
class Person(object): def __init__(self, name): print(f"init: '{self}' '{self.__class__.__name__}'") self.name = name def show_name(self): print(f"instance method: '{self}' '{self.__class__.__name__}'") @classmethod def from_occupation(cls, occupation): print(f"class method '{cls}' '{cls.__class__.__name__}'") @staticmethod def is_valid_occupation(param): print(f"static method '{param}' '{param.__class__.__name__}'") fb = Person('Foo Bar') fb.show_name() fb.from_occupation('Tailor') Person.from_occupation('Tailor') # This is how we should call it. fb.is_valid_occupation('Tailor') Person.is_valid_occupation('Tailor')
init: '<__main__.Person object at 0x7fb008f3a640>' 'Person' instance method: '<__main__.Person object at 0x7fb008f3a640>' 'Person' class method '<class '__main__.Person'>' 'type' class method '<class '__main__.Person'>' 'type' static method 'Tailor' 'str' static method 'Tailor' 'str'