pythonpython-3.xobjectscopestatic-methods

How to call static methods in functions? Scope of static methods


Suppose I create a class with a static method and want to run that method from another classes method or script function.

What is the scope of the static method?

ex:

def Class myClass:

     @staticmethod
     def mystaticmethod(input):
          print("blah")

def scriptfunc():
     myClass.mystaticmethod()

Is this valid?


Solution

  • What you have is valid.

    But too elaborate on the purpose of @staticmethod, here's Short answer:

    Declaring a @staticmethod would mean 2 things:

    1. You don't care about the properties or attributes of a method as it's independent of other classes,
    2. You do not require creating an __init__ or a super method to override it's content or attributes, and doesn't require a subclass/parent class to handle itself.