Decorator staticmethod Use Cases - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Decorator staticmethod Use Cases (/thread-18457.html) |
Decorator staticmethod Use Cases - Devarishi - May-18-2019 A method defined inside a class with the decorator @staticmethod can be executed without creating a class object. However, the same can be done even if the decorator was not used:className.MethodName() So, why should we bother about using @staticmethod?
RE: Decorator staticmethod Use Cases - Yoriz - May-18-2019 If you make an instance of a class that has a staticmethod that takes arguments with no decorator it will expect self as the first argument. class SomeClass: def somemethod(something): print(something) it_fails = SomeClass() it_fails.somemethod('fail') Using the @staticmethod stops it expecting self as the first argumentclass SomeClass: @staticmethod def somemethod(something): print(something) its_ok = SomeClass() its_ok.somemethod('Does not fail')
RE: Decorator staticmethod Use Cases - ichabod801 - May-18-2019 To expand on what Yoriz said, there are three basic use cases for class methods in Python:
RE: Decorator staticmethod Use Cases - Devarishi - May-20-2019 @Yoriz @ichabod801 Thanks to both of you for your generous explanations of the concept in question. The example given has helped me understand it better. Regards, Dev. |