I would like to have a method that is both a classmethod and an
instancemethod. So:
class MyClass(object):
@class_or_instance
def myfunc(cls_or_self):
pass
The semantics I'd like are:
When you call MyClass.myfunc, it gets passed a class
When you call MyClass().myfunc, it gets passed an instance
I'm sure I've seen some code to do this somewhere, but I can't find it
now. Any help appreciated.
Paul
IIRC, there's something quite similar in formencode.
> I would like to have a method that is both a classmethod and an
> instancemethod. So:
>
> class MyClass(object):
> @class_or_instance
> def myfunc(cls_or_self):
> pass
>
> The semantics I'd like are:
> When you call MyClass.myfunc, it gets passed a class
> When you call MyClass().myfunc, it gets passed an instance
class class_or_instance(object):
def __init__(self, fn):
self.fn = fn
def __get__(self, obj, cls):
if obj is not None:
return lambda *args, **kwds: self.fn(obj, *args, **kwds)
else:
return lambda *args, **kwds: self.fn(cls, *args, **kwds)
>>> class MyClass(object):
... @class_or_instance
... def myfunc(cls_or_self):
... return cls_or_self
...
>>> MyClass.myfunc()
<class '__main__.MyClass'>
>>> MyClass().myfunc()
<__main__.MyClass object at 0xb7a248cc>
You might want to use functools.wrap to return named functions rather
than unnamed lambdas, but you get the idea.
> class class_or_instance(object):
> def __init__(self, fn):
...
This works a treat, thank-you.
Paul
Just to polish a bit:
import functools
class ClassOrInstance(object):
def __init__(self, fn):
self._function = fn
self._wrapper = functools.wraps(fn)
def __get__(self, obj, cls):
return self._wrapper(functools.partial(self._function,
cls if obj is None else obj))
--Scott David Daniels
Scott....@Acm.Org
from types import MethodType
class ClassOrInstance(object):
def __init__(self, func):
self._func = func
def __get__(self, obj, cls):
return MethodType(self._func, cls if obj is None else obj, cls)
-Miles