Search code examples
pythonstatic-methods

Static methods - How to call a method from another method?


When I have regular methods for calling another method in a class, I have to do this

class test:
    def __init__(self):
        pass
    def dosomething(self):
        print "do something"
        self.dosomethingelse()
    def dosomethingelse(self):
        print "do something else"

but when I have static methods I can't write

self.dosomethingelse()

because there is no instance. What do I have to do in Python for calling a static method from another static method of the same class?


Solution

  • class.method should work.

    class SomeClass:
      @classmethod
      def some_class_method(cls):
        pass
    
      @staticmethod
      def some_static_method():
        pass
    
    SomeClass.some_class_method()
    SomeClass.some_static_method()