@classmethod and @staticmethod

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5168

    #1

    @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)








    @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.




    More...
Working...