出于特定的调试目的,我想包装任意对象的del函数来执行额外的任务,比如将对象的最后一个值写入文件.
理想情况下,我想编写monkey(x),它应该意味着删除x时会打印x的最终值
现在我认为del是一个类方法.所以以下是一个开始:
class Test:
def __str__(self):
return "Test"
def p(self):
print(str(self))
def monkey(x):
x.__class__.__del__=p
a=Test()
monkey(a)
del a
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要特定的对象,我想我需要动态地将它们的类重写为一个新的?此外我还需要这样做,因为我无法访问内置类型的del?
谁知道如何实现?
我正在使用相当大的OOP代码库,我想注入一些跟踪/日志记录。最简单的方法是在一些基类的某些方法周围引入一个装饰器,但是不幸的是装饰器没有被继承。
我确实尝试过以下操作:
def trace(fn):
def wrapper(instance, *args, **kwargs):
result = fn(instance, *args, **kwargs)
# trace logic...
return result
return wrapper
class BaseClass(object):
def __init__(self, ...):
...
self.__call__ = trace(self.__call__) # line added to end of method
Run Code Online (Sandbox Code Playgroud)
...并且当__call__方法被包裹(我可以从印刷实例信息发送到控制台看到)的wrapper预期不执行功能。
我还简要地研究了基于此答案的元类,但是它立即破坏了使用自省的系统其他部分,因此我认为这是不可行的。
还有其他方法可以强制这些装饰器围绕__call__从其继承的类的方法应用BaseClass吗?
我试图在引发异常后覆盖Python中Exception子类的打印输出,并且我没有运气实际调用我的覆盖.
def str_override(self):
"""
Override the output with a fixed string
"""
return "Override!"
def reraise(exception):
"""
Re-raise an exception and override its output
"""
exception.__str__ = types.MethodType(str_override, exception, type(exception))
# Re-raise and remove ourselves from the stack trace.
raise exception, None, sys.exc_info()[-1]
def test():
"""
Should output "Override!" Actually outputs "Bah Humbug"
"""
try:
try:
raise Exception("Bah Humbug")
except Exception, e:
reraise(e, "Said Scrooge")
except Exception, e:
print e
Run Code Online (Sandbox Code Playgroud)
知道为什么这实际上没有覆盖str方法吗?反思实例变量表明该方法实际上已被方法覆盖,但它就像Python一样拒绝通过print调用它.
我在这里错过了什么?