@classmethod and @staticmethod

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

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
pittsburgh-based-patterns-of-meaning-art-initiative-partners-with-metal,-iacmi-to-engage-k-12-students-in-steel,-metal-manufacturing

Pittsburgh-based Patterns of Meaning Art Initiative Partners with METAL, IACMI to Engage K-12 Students in Steel, Metal Manufacturing

Next Post
custom-domain-for-web-apps-on-cloud-run-–-how-to-set-up-application-load-balancer-on-gcp

Custom Domain for Web Apps on Cloud Run – How to Set Up Application Load Balancer on GCP

Related Posts