staticmethod() problems

Aum spam-me at no-thanks.com
Thu Jan 23 08:04:16 EST 2003


Erik Max Francis wrote:

> Aum wrote:
> 
>> I'm trying to find a way for a static method in a class to determine
>> if it
>> was called from the class, or from an instance.
> ...
>> Is there any way to do this without having to define 2 separate
>> methods?
> 
> 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'?

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.

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?

Secondly, I'm writing an API, for which the easiest usage is for the user to 
create n subclasses of a given class, and create a *single* instance of 
each subclass. Why? Because much of the API's capabilities are far more 
easily accessed by overriding default class methods.

Since there's only one instance of each class, I thought it would be good to 
allow the user to simply run a static method - the static method could 
detect if it's been called via an instance or statically. If statically, 
then it can self-instantiate before invoking other methods.

For example:

>>>class API_BaseClass:
...     blah blah
...
...     def run_(self, **parms):
...             do what is needed
...
...     def run(**parms):
...             if invoked via instance:
...                     i = instance object
...                     i.run_(**parms)
...             else # invoked statically
...                     c = this class' object
...                     c().run_(**parms)
...     run = staticmethod(run)
...

that outlines the base class provided by the API.
Now, onto the user's code:

>>>class mySubclass:
...     blah blah
...     override some stuff

At this point, the user has to manually instantiate, with:

>>>m = mySubclass()
>>>m.run(arg1=val1, arg2=val2,...)

But what would be wrong with providing the user with a way of simply
running the class, via:

>>>mySubclass(arg1=val1, arg2=val2,...)

Why force the user to go through the tiresome ritual of instantiating each 
of their subclasses once before invoking them?

IMO, it would be more within Python's spirit of freedom to allow such 
choices to the programmer. Especially in this case, where there is a 1:1 
mapping between subclasses and instances.

Regards
A





More information about the Python-list mailing list