150

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?

4
  • 5
    @pablo: you can't change the essence of the question !! End this question and start another one!!
    – jldupont
    Dec 7, 2009 at 14:25
  • ok, ok. do you mean delete the question? The answer is already there in Peter Hansen's comment
    – Pablo
    Dec 7, 2009 at 14:31
  • @pablo: you can't delete here that wouldn't be polite for everybody that contributed answers to your question. You need to accept an answer and create a new question.
    – jldupont
    Dec 7, 2009 at 14:33
  • 1
    @pablo: and just to be clear: an answer should be accepted for your original question formulation. Don't worry, you'll learn your way around here. Cheers :-)
    – jldupont
    Dec 7, 2009 at 14:37

7 Answers 7

217

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()
2
  • 1
    The call Test.static_method_to_call() should work from other non-static and non-class methods of this class aswell, right? (... and probably other classes too?)
    – gr4nt3d
    Mar 10, 2020 at 11:51
  • 1
    Yes, Test.static_method_to_call() will work from anywhere.
    – warvariuc
    Mar 10, 2020 at 19:37
104

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()
4
  • 26
    Advice should be added here: It is clear that classmethods have no drawbacks over staticmethods, and classmethods allows the method to be extended in the future to call other class methods. Thus it should always be preferred, even though there is no immediate need. Dec 7, 2009 at 14:24
  • 1
    @u0b34a0f6ae: I don't know if I am doing something good or not... but I defined a decorator inside a class. The decorator had to be a "staticmethod", not a "classmethod". Mar 20, 2013 at 18:41
  • @u0b34a0f6ae Are classmethods always thread safe though? In my use case I am using staticmethods in threads to prevent accidental access to the class which may not be thread safe.
    – aquavitae
    Mar 25, 2014 at 5:49
  • @u0b34a0f6ae always is a bit strong. For example using PySpark staticmethods can be used in rdd jobs and classmethods cannot. May 31, 2017 at 14:57
12

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()
5
  • 1
    you can only use cls.dosomethingelse() from within the class definition.
    – jldupont
    Dec 7, 2009 at 13:31
  • 3
    To be more accurate, you can only use cls.dosomethingelse() from within the class method itself. Just like you can only use self from within an instance method itself. Dec 7, 2009 at 13:35
  • 1
    sorry, I wrongly wrote the question. OOPS!. I wanted to write "How do I have to do in Python for calling an instance method from another static method of the same class" and not "How do I have to do in Python for calling an static method from another static method of the same class"
    – Pablo
    Dec 7, 2009 at 14:19
  • @pablo: In this case, you need to write another question and finish this one.
    – jldupont
    Dec 7, 2009 at 14:23
  • @jldupont , I already have the answer but in one comment. What should I do because I can't accept it but it would be a waste to erase the question, wouldnt be?
    – Pablo
    Dec 7, 2009 at 14:29
6

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
1
  • 2
    this should be marked as a correct answer. Works for me, thx.
    – Alex Sham
    Feb 25 at 22:17
3

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 ;)

2

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
1
  • 2
    This doesn't seem right - but the meaning here is vague without an example, so it's hard to tell. Mainly, "class methods" don't have to be called "from within an instance"... you can call MyClass.class_method() from anywhere that knows about MyClass (maybe you were thinking of "instance methods"?)
    – mltsy
    Sep 17, 2020 at 18:06
1

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()
2
  • 3
    IMO this approach is notably worse than just using @classmethod to declare your methods, and using cls.dosomethingelse() within cls.dosomething() -- worse in terms of both performance (instantiating objects just to get access to a static method) and readability (it's not straightforward, and I imagine if that sort of pattern became boilerplate, the whole codebase could become quite difficult for a mere human to parse)
    – Kyle Wild
    Oct 7, 2011 at 20:55
  • As I read somewhere else at SO, instead of initializing instance directly to call a method, you can just call it - it will initizlize itself on its own.
    – Nakilon
    May 8, 2013 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.