11

I can't find if it's possible to call a non-static method from a static one in Python.

Thanks

EDIT: Ok. And what about static from static? Can I do this:

class MyClass(object):

    @staticmethod
    def static_method_one(cmd):
    ...

    @staticmethod
    def static_method_two(cmd):
        static_method_one(cmd)
legesh
  • 2,097
  • 5
  • 19
  • 23
  • Surely you can call a static method from another one. I mean you have to ,when you cannot call a non-static one from a static one. – Shagun Pruthi Dec 14 '18 at 08:07

5 Answers5

15

It's perfectly possible, but not very meaningful. Ponder the following class:

class MyClass:
    # Normal method:
    def normal_method(self, data):
        print "Normal method called with instance %s and data %s" % (self, data)

    @classmethod
    def class_method(cls, data):
        print "Class method called with class %s and data %s" % (cls, data)

    @staticmethod
    def static_method(data):
        print "Static method called with data %s" % (data)

Obviously, we can call this in the expected ways:

>>> instance = MyClass()
>>> instance.normal_method("Success!")
Normal method called with instance <__main__.MyClass instance at 0xb7d26bcc> and data Success!

>>> instance.class_method("Success!")
Class method called with class __main__.MyClass and data Success!

>>> instance.static_method("Success!")
Static method called with data Success!

But also consider this:

>>> MyClass.normal_method(instance, "Success!")
Normal method called with instance <__main__.MyClass instance at 0xb7d26bcc> and data Success!

The syntax instance.normal_method() is pretty much just a "shortcut" for MyClass.normal_method(instance). That's why there is this "self" parameter in methods, to pass in self. The name self is not magical, you can call it whatever you want.

The same trick is perfectly possible from withing a static method. You can call the normal method with an instance as first parameter, like so:

    @staticmethod
    def a_cool_static_method(instance, data):
        print "Cool method called with instance %s and data %s" % (instance, data)
        MyClass.normal_method(instance, data)
        MyClass.class_method(data)
        MyClass.static_method(data)

>>> instance.a_cool_static_method(instance, "So Cool!")
Cool method called with instance <__main__.MyClass instance at 0xb7d26bcc> and data So Cool!
Normal method called with instance <__main__.MyClass instance at 0xb7d26bcc> and data So Cool!
Class method called with class __main__.MyClass and data So Cool!
Static method called with data So Cool!

So the answer is yes, you can cal non-static methods from static methods. But only if you can pass in an instance as first parameter. So you either have to generate it from inside the static method (and in that case you are probably better off with a class method) or pass it in. But if you pass in the instance, you can typically just make it a normal method.

So you can, but, it's pretty pointless.

And that then begs the question: Why do you want to?

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
7

After the other answers and your follow-up question - regarding static method from static method: Yes you can:

>>> class MyClass(object):
    @staticmethod
    def static_method_one(x):
        return MyClass.static_method_two(x)
    @staticmethod
    def static_method_two(x):
        return 2 * x


>>> MyClass.static_method_one(5)
10

And, in case you're curious, also yes for class method from class method (easy to test this stuff in the interpreter - all this is cut and pasted from Idle in 2.5.2) [**EDITED to make correction in usage pointed out by others**]:

>>> class MyClass2(object):
    @classmethod
    def class_method_one(cls, x):
        return cls.class_method_two(x)
    @classmethod
    def class_method_two(cls, x):
        return 2 * x


>>> MyClass2.class_method_one(5)
10
Anon
  • 11,870
  • 3
  • 23
  • 19
  • Second example should call `cls.class_method_two(x)`; that also shows why the OP should use `classmethod`, not `staticmethod`. – u0b34a0f6ae Sep 06 '09 at 13:04
  • @u0b34a0f6ae Depends on what they want to do. If they want to pass in the proper subclass, then a class method is appropriate. If they *always* want to use `MyClass` then there is little point in passing in the subclass (as a classmethod would). – DylanYoung Jul 16 '18 at 16:00
4

Use class methods, not static methods. Why else put it inside a class?

class MyClass(object):

    @classmethod
    def static_method_one(cls, cmd):
    ...

    @classmethod
    def static_method_two(cls, cmd):
        cls.static_method_one(cmd)
u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
2

When in a static method, you don't have a self instance: what object are you calling the non-static methods on? Certainly if you have an instance lying around, you can call methods on it.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
0

It's not possible withotut the instance of the class. You could add a param to your method f(x, y, ..., me) and use me as the object to call the non-static methods on.

Marcel Jackwerth
  • 53,948
  • 9
  • 74
  • 88
  • It is possible. Read the above answers. Just not straightforward to do, but you can do it . MyClass.normal_method(instance, data) – Shagun Pruthi Dec 14 '18 at 08:12