calling one staticmethod from another

U

Ulrich Eckhardt

Hi!

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?

Uli
 
E

Ethan Furman

Ulrich said:
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~
 
D

Dave Angel

Hi!

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?

Uli

I'd think the obvious solution is to move both the functions outside of
the class. I haven't figured out the justification for staticmethod,
except for java or C++ converts.

But if you like the staticmethod for other reasons, why is it you can't
just use
C.g()

?
 
I

Ian Kelly

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.

It fails if the staticmethod being called has been overridden in a
subclass, though. I think the only correct way to do it with
inheritance is by replacing it with a classmethod, as the OP
suggested.
 
U

Ulrich Eckhardt

Am 30.10.2012 14:47, schrieb Dave Angel:
I'd think the obvious solution is to move both the functions outside of
the class. I haven't figured out the justification for staticmethod,
except for java or C++ converts.

Although I come from a C++ background, I think static functions have
solid reasons that are not just based on my habits. When I see a static
function in C++, I know that it is a function, not a method, so the only
context it could interact with is also static (inside a namespace,
including the global namespace or statically inside the class) or passed
as parameters. Further, the function itself is inside a class (possibly
even private), so it should only be of interest in the context of that
class or instances thereof and doesn't collide with other functions.

In summary, putting utility code into a function reduces the context it
interacts with. Putting that utility function as staticmethod inside a
class further reduces the context of that function. Together, this also
reduces the complexity of the code, making it easier to write and read.

But if you like the staticmethod for other reasons, why is it you can't
just use
C.g()
?

This works. It's just that I find it a bit inconvenient/ugly to repeat
the classname inside a class. But hey, coming from C++ I have gotten
used to always writing "self." to call one member function from another,
so I'll probably survive this one, too. ;)


Greetings!

Uli
 
M

Mark Lawrence

Hi!

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?

Uli

I hope that you find these useful.

http://dirtsimple.org/2004/12/python-is-not-java.html
http://dirtsimple.org/2004/12/java-is-not-python-either.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top