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

Question:

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 an static method from another static method of the same class?

Asked By: Pablo

||

Answers:

NOTE – it looks like the question has changed some. The answer to the question of how you call an instance method from a static method is that you can’t without passing an instance in as an argument or instantiating that instance inside the static method.

What follows is mostly to answer “how do you call a static method from another static method”:

Bear in mind that there is a difference between static methods and class methods in Python. A static method takes no implicit first argument, while a class method takes the class as the implicit first argument (usually cls by convention). With that in mind, here’s how you would do that:

If it’s a static method:

test.dosomethingelse()

If it’s a class method:

cls.dosomethingelse()
Answered By: Jason Baker

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()
Answered By: jldupont

You can’t call non-static methods from static methods, but by creating an instance inside the static method.

It should work like that

class test2(object):
    def __init__(self):
        pass

    @staticmethod
    def dosomething():
        print "do something"
        # Creating an instance to be able to
        # call dosomethingelse(), or you
        # may use any existing instance
        a = test2()
        a.dosomethingelse()

    def dosomethingelse(self):
        print "do something else"

test2.dosomething()
Answered By: Ahmad Dwaik

OK the main difference between class methods and static methods is:

  • class method has its own identity, that’s why they have to be called from within an INSTANCE.
  • on the other hand static method can be shared between multiple instances so that it must be called from within THE CLASS
Answered By: Ahmad Dwaik

How do I have to do in Python for calling an static method from another static method of the same class?

class Test():
    @staticmethod
    def static_method_to_call():
        pass

    @staticmethod
    def another_static_method():
        Test.static_method_to_call()

    @classmethod
    def another_class_method(cls):
        cls.static_method_to_call()
Answered By: warvariuc

If these don’t depend on the class or instance, then just make them a function.

As this would seem like the obvious solution. Unless of course you think it’s going to need to be overwritten, subclassed, etc. If so, then the previous answers are the best bet. Fingers crossed I won’t get marked down for merely offering an alternative solution that may or may not fit someone’s needs ;).

As the correct answer will depend on the use case of the code in question 😉

Answered By: AppHandwerker

You can call __class__.dosomethingelse() instead of self.dosomethingelse() if your called function is in the same class as the caller static method.

class WithStaticMethods:
    @staticmethod
    def static1():
        print("This is first static")

    @staticmethod
    def static2():
        # WithStaticMethods.static1()
        __class__.static1()
        print("This is static too")


WithStaticMethods.static2()

prints:

This is first static
This is static too
Answered By: Emil Vatai
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.