instance + classmethod question
Laszlo Zsolt Nagy
gandalf at designaproduct.biz
Sun Dec 11 00:12:06 EST 2005
Hello Steven,
I already implemented this using the form
@classmethod
def methodname(cls,other_params,self=None)
but your example code looks so neat! This is exactly what I needed. :-)
In my methods, most code is about string manipulation and calling other
classmethods.
There are only a few places where I can use an instance, but it is not
required.
I would like to reuse as most code as possible, so I do not want to
create two different
methods. That would result in duplicating code. Now the only problem is
how I name this.
It is not a classmethod, but it is also not a normal method. All right,
it is a
"ClassOrInstanceMethod". Amazing! Probably Python is the only language
that is
flexible enough to do this. :-)
Thanks again!
Laszlo
Steven Bethard wrote:
>Laszlo Zsolt Nagy wrote:
>
>
>> Hello,
>>
>>Is it possible to tell, which instance was used to call the classmethod
>>that is currently running?
>>
>>
> >>> class ClassOrInstanceMethod(object):
>... def __init__(self, func):
>... self.func = func
>... self.classfunc = classmethod(func)
>... def __get__(self, obj, objtype=None):
>... func = obj is None and self.classfunc or self.func
>... return func.__get__(obj, objtype)
>...
>
>
More information about the Python-list
mailing list