Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Python’s @classmethod and @staticmethod Explained

For beginners who are learning object-oriented programming in Python, it is very essential to have a good grasp over Class method and static method for writing more optimized and reusable code. Also, it is very common for even experienced programmers coming from different languages to get confused between these two. In this article, we will develop a better understanding of the class method and static methods in Python. Static Method in Python A @staticmethod is a method that knows nothing about the class or instance it was called on unless explicitly given. It just gets the arguments that were passed, no implicit first argument and It’s definition is immutable via inheritance. In simpler words a @staticmethod  is nothing more than a regular function defined inside a class that doesn’t have access to the instance therefore It is callable without instantiating the class. Syntax: class ClassName: @staticmethod def method_name(arg1, arg2, ...): ... We use the @staticmethod decorator for defining a static method in Python, here you can observe that the static method is not taking self as an argument for the …

The post Python’s @classmethod and @staticmethod Explained appeared first on Django Central.



This post first appeared on Django Central, please read the originial post: here

Share the post

Python’s @classmethod and @staticmethod Explained

×

Subscribe to Django Central

Get updates delivered right to your inbox!

Thank you for your subscription

×