calling one staticmethod from another

Ethan Furman ethan at stoneleaf.us
Tue Oct 30 09:41:05 EDT 2012


Ulrich Eckhardt wrote:
> I can call a staticmethod f() of class C like "C.f()" or with an 
> instance like "C().f()". Inside that staticmethod, I have neither the 
> class (at least not the original one) nor do I have an instance, so I 
> can't call a different staticmethod from the same class. The obvious 
> solution is to make this a classmethod instead, with a mostly-unused 
> "cls" parameter.
> 
> Am I missing something?

class Spam():
     @staticmethod
     def green():
         print('on a train!')
     @staticmethod
     def question():
         print('would you, could you', end='')
         Spam.green()

It can be a pain if you change the class name, but it is certainly one way to do it.

~Ethan~



More information about the Python-list mailing list