DEV Community

Ganesan Krishnan
Ganesan Krishnan

Posted on

@classmethod and @staticmethod

@classmethod generally used as factory methods, takes in a parameter representing the class

class Book:
    TYPE = ('tech', 'fiction')
    def __init__(self, name, book_type):
        self.name = name
        self.book_type = book_type

    @classmethod 
    def tech_book(cls, name):
        cls(name, cls.TYPE[0])

    @staticmethod 
    def print_book(book):
      send_to_printer(book)

Enter fullscreen mode Exit fullscreen mode

@staticmethod generally refers to simple method that happens to be inside the class and generally provides some utility to the class that perform some operation without changing the data stored in the object.

Top comments (0)