【发布时间】:2011-11-18 16:10:06
【问题描述】:
我想创建一个 Python 类装饰器 (*),它能够无缝地封装类可能具有的所有方法类型:实例、类和静态。
这是我现在拥有的代码,其中有破坏它的部分已注释:
def wrapItUp(method):
def wrapped(*args, **kwargs):
print "This method call was wrapped!"
return method(*args, **kwargs)
return wrapped
dundersICareAbout = ["__init__", "__str__", "__repr__"]#, "__new__"]
def doICareAboutThisOne(cls, methodName):
return (callable(getattr(cls, methodName))
and (not (methodName.startswith("__") and methodName.endswith("__"))
or methodName in dundersICareAbout))
def classDeco(cls):
myCallables = ((aname, getattr(cls, aname)) for aname in dir(cls) if doICareAboutThisOne(cls, aname))
for name, call in myCallables:
print "*** Decorating: %s.%s(...)" % (cls.__name__, name)
setattr(cls, name, wrapItUp(call))
return cls
@classDeco
class SomeClass(object):
def instanceMethod(self, p):
print "instanceMethod: p =", p
@classmethod
def classMethod(cls, p):
print "classMethod: p =", p
@staticmethod
def staticMethod(p):
print "staticMethod: p =", p
instance = SomeClass()
instance.instanceMethod(1)
#SomeClass.classMethod(2)
#instance.classMethod(2)
#SomeClass.staticMethod(3)
#instance.staticMethod(3)
我在尝试完成这项工作时遇到了两个问题:
- 在遍历所有可调用对象时,如何确定它是实例、类还是静态类型?
- 如何使用针对每种情况正确调用的正确包装版本覆盖该方法?
目前,此代码会生成不同的TypeErrors,具体取决于已注释的 sn-p 未注释的内容,例如:
TypeError: unbound method wrapped() must be called with SomeClass instance as first argument (got int instance instead)TypeError: classMethod() takes exactly 2 arguments (3 given)
(*):如果你是decorating the methods directly,同样的问题会简单得多。
【问题讨论】:
-
很高兴看到不同的开发人员如何处理同样复杂的问题。
-
@wberry:是的,我已经在阅读当前的答案并注意到很难选择“正确”的答案。
标签: python reflection decorator instrumentation