class or instance method

P

Paul Johnston

Hi,

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
 
B

Bruno Desthuilliers

Paul Johnston a écrit :
Hi,

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.

IIRC, there's something quite similar in formencode.
 
H

Hrvoje Niksic

Paul Johnston said:
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_or_instance
.... def myfunc(cls_or_self):
.... return cls_or_self
....<__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.
 
M

Miles Kaufmann

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))



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top