staticmethod() problems

Thomas Heller theller at python.net
Thu Jan 23 14:28:27 CET 2003


> But there's a real problem with staticmethod. Within a staticmethod 
> function, I can't see any way to access other static attributes of the 
> class - methods or attributes - without knowing the class' name.

You'd have to use a classmethod if you want to do this, that's what
they are fore (in Python, at least).

> 
> With this restriction, python's static method implementation falls short of 
> that of C++ (I won't say j--- because I don't know j---). At least in C++, 
> you can subclass a class with static methods, and within the subclass, the 
> inherited methods can access other static attributes within the class, 
> without having to explicitly specify the class.
> 
> > Maybe if you back up one step and tell us why you need to know this we
> > could suggest a better solution.
> 
> Firstly, python excels at empowering the programmer, and removing a vast 
> number of the restrictions which make other languages a total pain. So why 
> *shouldn't* there be a way for a static method to know (if it wants to) if 
> it's been invoked via the class or via an instance? And to determine the 
> exact class or subclass from which it's been invoked?

I posted a recipe some time ago which IMO allows you to do it:

http://tinyurl.com/4shq

HTH,

Thomas




More information about the Python-list mailing list