Erik Max Francis
max at alcyone.com
Fri Jan 24 01:16:25 CET 2003
> Erik Max Francis wrote:
> > The very purpose of staticmethod is to create a method that can be
> > invoked from an instance or a class and won't know the difference,
> > or
> > care. In some sense, what you're trying to do is at odds with the
> > purpose of classmethod in the first place.
> Don't you mean 'staticmethod'?
Absolutely; that was a brainfart on my part, my apologies.
> One of the purposes of staticmethod, yes.
> 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.
That's not a "problem," that's really staticmethod's purpose. C++
(which you reference later) is no different; a C++ static member
function doesn't know anything about the class it's attached to. It's
just a function which is associated with a class, and can either be
called from a class (C::memberFunction()) or an instance
> 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
> you can subclass a class with static methods, and within the subclass,
> inherited methods can access other static attributes within the class,
> without having to explicitly specify the class.
That's true, but Python is not C++, nor is the reverse the case. C++
has more complex scoping than Python, which is why this is the case.
staticmethods are a case where the intent is to tie a function to a
class such that it is can be called.
If you really do want to access the class that the staticmethod is tied
to, then maybe a classmethod would be more apprporiate (this time my
mention of term is not a brainfart :-).
> Firstly, python excels at empowering the programmer, and removing a
> number of the restrictions which make other languages a total pain. So
> *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
> exact class or subclass from which it's been invoked?
The best answer I can give you is that, "Because that's not what
staticmethods were intended to be used for." If you want the function
to know the class it's associated with, then use a classmethod.
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ An undevout astronomer is mad.
\__/ Edward Young
Bosskey.net: Quake III Arena / http://www.bosskey.net/q3a/
A personal guide to Quake III Arena.
More information about the Python-list