159

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pablo
  • 4,821
  • 12
  • 52
  • 82
  • 5
    @pablo: you can't change the essence of the question !! End this question and start **another one**!! – jldupont Dec 07 '09 at 14:25
  • ok, ok. do you mean delete the question? The answer is already there in Peter Hansen's comment – Pablo Dec 07 '09 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 07 '09 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 07 '09 at 14:37

7 Answers7

229

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()
warvariuc
  • 57,116
  • 41
  • 173
  • 227
  • 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?) – BadAtLaTeX Mar 10 '20 at 11:51
  • 1
    Yes, `Test.static_method_to_call()` will work from anywhere. – warvariuc Mar 10 '20 at 19:37
105

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()
Pierre
  • 12,468
  • 6
  • 44
  • 63
jldupont
  • 93,734
  • 56
  • 203
  • 318
  • 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. – u0b34a0f6ae Dec 07 '09 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". – André Caldas Mar 20 '13 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 '14 at 05:49
  • 1
    @u0b34a0f6ae *always* is a bit strong. For example using PySpark staticmethods can be used in rdd jobs and classmethods cannot. – Laurens Koppenol May 31 '17 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()
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
  • 1
    you can only use `cls.dosomethingelse()` from within the class definition. – jldupont Dec 07 '09 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. – Jason Baker Dec 07 '09 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 07 '09 at 14:19
  • @pablo: In this case, you need to write **another** question and finish this one. – jldupont Dec 07 '09 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 07 '09 at 14:29
7

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
Emil Vatai
  • 2,298
  • 1
  • 18
  • 16
  • 2
    this should be marked as a correct answer. Works for me, thx. – Alex Sham Feb 25 '23 at 22:17
  • If you are going to call a static method from the same class in another static method, just use `@classmethod` instead and do `cls.static1()`. That would be more explicit and that's the idea of `@classmethod` decorator. No need for workarounds like `__class__.static1`. – warvariuc Jul 28 '23 at 08:05
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 ;)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AppHandwerker
  • 1,758
  • 12
  • 22
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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ahmad Dwaik
  • 963
  • 1
  • 9
  • 13
  • 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 '20 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()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmad Dwaik
  • 963
  • 1
  • 9
  • 13
  • 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 07 '11 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 08 '13 at 11:30