Python Forum
Decorator staticmethod Use Cases
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decorator staticmethod Use Cases
#1
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?
Reply
#2
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')
Error:
TypeError: somemethod() takes 1 positional argument but 2 were given
Using the @staticmethod stops it expecting self as the first argument
class SomeClass:
    @staticmethod
    def somemethod(something):
        print(something)

its_ok = SomeClass()
its_ok.somemethod('Does not fail')
Output:
Does not fail
Reply
#3
To expand on what Yoriz said, there are three basic use cases for class methods in Python:
  • You need access to the instance: use a method with no decorator, and the instance will be automatically provided as the first parameter. This is your standard run of the mill method.
  • You need access to the class: use a method with the classmethod decorator, and the class will be automatically provided s the first parameter. I often use these for alternate constructors.
  • You don't need access to the instance or the method: use a method with the staticmethod decorator. This is for code you want to keep associated with the code, but isn't necessarily focused on a particular instance or the class itself.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
@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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  @staticmethod and @classmethod akbarza 2 447 Jan-01-2024, 03:43 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 480 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Curious about decorator syntax rjdegraff42 14 1,977 May-03-2023, 01:21 PM
Last Post: rjdegraff42
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,496 Aug-18-2021, 06:08 PM
Last Post: muzikman
  Uses cases for list comprehension (encountered while doing PyBite #77) Drone4four 3 2,739 Sep-25-2020, 12:14 PM
Last Post: ndc85430
  decorator adamfairhall 0 1,540 Aug-18-2020, 08:38 AM
Last Post: adamfairhall
  Use of @property decorator ruy 16 6,378 Jun-09-2020, 05:29 PM
Last Post: buran
  Iterating through lists with different letter cases fatherted99 2 1,833 Jun-07-2020, 01:22 PM
Last Post: deanhystad
  How can we override decorator? bhojendra 2 9,261 May-12-2019, 11:15 PM
Last Post: ichabod801
  How I can recognize that member is classmethod of staticmethod? AlekseyPython 0 1,806 Feb-17-2019, 07:01 AM
Last Post: AlekseyPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020