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

When I have the usual methods for calling another method in the 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 cant write

 self.dosomethingelse() 

because there is no instance. How do I do in Python to call a static method from another static method of the same class?

Edit: what a mess. Ok, I edited the question to the original question. I already have the answer to the second question, which is in the commentary of Peter Hansen. If you think that I should open another question for the answer that I already have, plz tell me.

+75
python static-methods
Dec 07 '09 at 13:23
source share
6 answers

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() 
+69
Dec 07 '09 at 13:26
source share

How do I do in Python to call a 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() 
+103
Nov 05 '10 at 8:08
source share

NOTE. It seems that the question has changed. The answer to the question of how you call an instance method from a static method is that you cannot pass an instance as an argument or create an instance of this instance inside a static method.

Next, the answer is "how do you call a static method from another static method":

Keep in mind that there is a difference between static methods and class methods in Python. The static method does not accept the implicit first argument, while the class method accepts the class as the implicit first argument (usually cls by convention). With that in mind, here is how you do it:

If this is a static method:

 test.dosomethingelse() 

If this is a class method:

 cls.dosomethingelse() 
+8
Dec 07 '09 at 13:25
source share

OK, the main difference between class methods and static:

  • A class method has its own identity, so they must be called from within.
  • on the other hand, a static method can be shared across multiple instances, so it must be called from THE CLASS
+5
Dec 07 '09 at 15:13
source share

You cannot use non-static methods from static methods, but by creating an instance inside a static method .... it should work this way

 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 you can help :)

+1
Dec 07 '09 at 14:48
source share

If they are class or instance independent, then why not just make them a function? As it would seem an obvious solution. Unless, of course, you think you need to rewrite it, a subclass, etc. If so, then the best answers will respond to previous answers. Fingers crossed, I will not mark, simply offering an alternative solution that may or may not meet the needs of some people;).

Since the correct answer will depend on the use case of the code in question;) Enjoy

0
May 30 '17 at 10:01
source share



All Articles