题
假设我们有一个元类 CallableWrappingMeta
,它遍历一个新类的主体,用一个类 InstanceMethodWrapper
包装它的方法:
import types
class CallableWrappingMeta(type):
def __new__(mcls, name, bases, cls_dict):
for k, v in cls_dict.iteritems():
if isinstance(v, types.FunctionType):
cls_dict[k] = InstanceMethodWrapper(v)
return type.__new__(mcls, name, bases, cls_dict)
class InstanceMethodWrapper(object):
def __init__(self, method):
self.method = method
def __call__(self, *args, **kw):
print "InstanceMethodWrapper.__call__( %s, *%r, **%r )" % (self, args, kw)
return self.method(*args, **kw)
class Bar(object):
__metaclass__ = CallableWrappingMeta
def __init__(self):
print 'bar!'
我们的虚拟包装器只是在它们进来时打印参数。但是你会注意到一些显着的东西:该方法没有通过实例对象接收器,因为即使 InstanceMethodWrapper
是可调用的,它也是在类创建期间(在我们的元类完成后),不会将其视为一个函数,以便转换为实例方法。
一个潜在的解决方案是使用装饰器而不是类来包装方法 - 该函数将成为实例方法。但在现实世界中, InstanceMethodWrapper
要复杂得多:它提供API并发布方法调用事件。一个班级更方便(而且性能更高,并不重要)。
我也尝试了一些死胡同。子类化 types.MethodType
和 types.UnboundMethodType
没有去任何地方。有点内省,看起来它们是 type
的后代。所以我尝试将两者都用作元类,但也没有运气。情况可能是他们作为元类有特殊要求,但此时我们似乎处于无证领域。
有什么想法吗?
解决方案
只需使用 __ get __
(这完全可以只是 return self
)来丰富 InstanceMethodWrapper
类 - 也就是说,将该类放入描述符类型,以便其实例是描述符对象。请参阅 http://users.rcn.com/python/download/Descriptor.htm用于背景和细节。
其他提示
编辑:我再次撒谎。函数上的 __?attr __
属性是只读的,但是在分配时显然并不总是抛出 AttributeException
异常?我不知道。回到原点!
编辑:这实际上并没有解决问题,因为包装函数不会将属性请求代理到 InstanceMethodWrapper
。当然,我可以在装饰器中点击 __?attr __
属性 - 而这正是我现在正在做的 - 但这很难看。更好的想法是非常受欢迎的。
当然,我立刻意识到将一个简单的装饰器与我们的类结合起来就可以了:
def methodize(method, callable):
"Circumvents the fact that callables are not converted to instance methods."
@wraps(method)
def wrapper(*args, **kw):
return wrapper._callable(*args, **kw)
wrapper._callable = callable
return wrapper
然后将装饰器添加到元类中对 InstanceMethodWrapper
的调用中:
cls_dict[k] = methodize(v, InstanceMethodWrapper(v))
噗。有点倾斜,但它有效。
我猜你正在尝试使用自定义函数创建一个包装类中每个方法的元类。
这是我的版本,我认为它有点不那么倾斜。
import types
class CallableWrappingMeta(type):
def __new__(mcls, name, bases, cls_dict):
instance = type.__new__(mcls, name, bases, cls_dict)
for k in dir(instance):
v = getattr(instance, k)
if isinstance(v, types.MethodType):
setattr(instance, k, instanceMethodWrapper(v))
return instance
def instanceMethodWrapper(function):
def customfunc(*args, **kw):
print "instanceMethodWrapper(*%r, **%r )" % (args, kw)
return function(*args, **kw)
return customfunc
class Bar(object):
__metaclass__ = CallableWrappingMeta
def method(self, a, b):
print a,b
a = Bar()
a.method("foo","bar")
我认为你需要更具体地解决你的问题。最初的问题涉及包装函数,但你后来的回答似乎是讨论保留函数属性,这似乎是一个新因素。如果您更清楚地阐明您的设计目标,则可能更容易回答您的问题。