views:

769

answers:

6

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. How do I have to do in Python for calling an static method from another static method of the same class?

Edit: what a mess. Ok, I edited back the question to the original question. I already have the answer to the second question that's in Peter Hansen's comment. If you think I should open another question for an answer I already have, plz tell me.

+2  A: 

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
you can only use `cls.dosomethingelse()` from within the class definition.
jldupont
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
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
@pablo: In this case, you need to write **another** question and finish this one.
jldupont
@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
+6  A: 

class.method should work.

class SomeClass:
  @classmethod
  class some_class_method(cls):
    pass

  @staticmethod
  class some_static_method():
    pass

SomeClass.some_class_method()
SomeClass.some_static_method()
jldupont
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.
kaizer.se
**NOTE** @pablo **changed** the question !!!!!
jldupont
A: 

ok, this is what I have

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

    @staticmethod
    def dosomething():
        print "do something"
        test2.dosomethingelse()

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

a = test2
a.dosomething()

if I call dosomethingelse from dosomething as test2.dosomethingelse() I get " unbound method dosomethingelse() must be called with test2 instance as first argument (got nothing instead)"

if I call dosomethingelse from dosomething as self.dosomethingelse() I get global name 'self' is not defined.

BTW, arent in Python Static methods what in other languages are called Class methods?

http://code.activestate.com/recipes/52304/

Pablo
No, static methods are *not* class methods. Use @classmethod() to get a class method, and make sure your first argument is called "cls" (or some such) instead of "self". But you'll never be able to call an instance method from a class method or a static method, unless you pass in an instance as a separate argument.
Peter Hansen
In other languages instante methods requires you to instantiate the class to call them and class methods don't. How are they called in Python if there is any paralelism? what's the difference in Python between a class method and a static method?
Pablo
Since I wrongly wrote the question and edit it my accepted answer should be your comment now. Do you want to write your comment as an answer for me to accept it and erase your comment?
Pablo
+3  A: 

you cant 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 instace
    a=test2()
    a.dosomethingelse()

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

test2.dosomething()

hope that will help you :)

Ahmad Dwaik
+2  A: 

ok the main deffirence between class methods and static methods is: class method has its own identity, thats 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

Ahmad Dwaik