Sorry if it's too late to be useful, but here's another stab at this.
class ClassProperty(object):
'''A descriptor for the named classmethod.'''
def __get__(self, cls, owner):
return getattr(cls,
self.name)()
class DynamicPropertiesMetaclass(type):
help = ClassProperty('_help')
def with_help(method, _cache={}):
'''Returns a proxy for method which has a dynamic "help"
property.'''
@property
def proxy_for_method(self):
try:
return _cache[(method, self)]
except KeyError:
class Proxy(object):
__metaclass__ = DynamicPropertiesMetaclass
__name__ = method.__name__
__doc__ = method.__doc__
__class__ = method.__class__
def __new__(cls, *args, **kwargs):
return method(self, *args, **kwargs)
@classmethod
def _help(cls, *args):
return '%s %s (%s)' % (method.help,
self.name,
method.__doc__)
_cache[(method, self)] = Proxy
return Proxy
return proxy_for_method
# ----
class Test(object):
''' yay '''
def __init__(self, name='world'):
self.name = name
def one(self):
''' method one '''
return (1, self,
self.name) # changed to test "self"
one.help = 'hello'
Test.one = with_help(Test.one) # or "@with_help" at method
declaration
t1 = Test('Peter')
t2 = Test('Eva')
print repr(t1.one.help)
print repr(t2.one.help)
print t1.one()
print t2.one()
Produces:
'hello Peter ( method one )'
'hello Eva ( method one )'
(1, <__main__.Test object at 0x7fd318fa6d10>, 'Peter')
(1, <__main__.Test object at 0x7fd318fa6d50>, 'Eva')
Tom