+ 1
Skipping @staticmethod decorator
With the decorator @staticmethod we can declare a static method of a class like this: class MyClass: @staticmethod def my_static_method(): print("my_static_method is called.") Then the static method can be accessed like this: MyClass.my_static_method() Now the thing is if I skip the @staticmethod decorator, nothing seems is being changed. The method still behaves like a static method of a class: you can call it from the outside the class using the class name, but not like a standard function . The question is if specifying @staticmethod decorator or skipping it completely gives any difference.
1 Resposta
+ 2
Python is all about reducing the havoc unleashed by long codes on the programmer.
@staticmethod are by design restricted from directly accessing the state/ attributes of the class or their instance. This let's any sane programmer create a safezone for such methods/functions which, though an integral part of the class are not required to know of it's attributes/state. This will help in debugging and expansion in the long run. In short it maintains sanity.
Other than the above, I like to believe that @staticmethod is redundant and useless so far as OOP in python is considered
Also, take note that you can work around to getting class/obj attributes from @staticmethod by using the class_name.attribute...