Wednesday, September 9, 2020

Abhijeet Pal: 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.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...