calling one staticmethod from another

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Tue Oct 30 11:39:49 EDT 2012


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




More information about the Python-list mailing list