如何增强 Python 对象的方法?
我有一个 Spam 对象列表:
class Spam:
def update(self):
print('updating spam!')
其中一些可能是 SpamLite 对象:
class SpamLite(Spam):
def update(self):
print('this spam is lite!')
Spam.update(self)
我希望能够从列表中获取任意对象,并向其更新方法添加一些内容,例如:
def poison(spam):
tmp = spam.update
def newUpdate(self):
print 'this spam has been poisoned!'
tmp(self)
spam.update = newUpdate
我想要 spam.update()现在要么打印:
this spam has been poisoned!
updating spam!
要么
this spam has been poisoned!
this spam is lite!
updating spam!
取决于它是垃圾邮件还是垃圾邮件。
但这不起作用,因为 spam.update() 不会自动传递 self 参数,并且因为如果 tmp 离开范围或发生更改,那么它不会调用旧的更新。我有办法做到这一点吗?
I have a list of Spam objects:
class Spam:
def update(self):
print('updating spam!')
some of them might be SpamLite objects:
class SpamLite(Spam):
def update(self):
print('this spam is lite!')
Spam.update(self)
I would like to be able to take an arbitrary object from the list, and add something to it's update method, something like:
def poison(spam):
tmp = spam.update
def newUpdate(self):
print 'this spam has been poisoned!'
tmp(self)
spam.update = newUpdate
I want spam.update() to now either print:
this spam has been poisoned!
updating spam!
or
this spam has been poisoned!
this spam is lite!
updating spam!
depending on whether it was a SpamLite or just a Spam.
But that doesn't work, because spam.update() won't pass in the self argument automatically, and because if tmp leaves scope or changes then it won't call the old update. Is there a way I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
完整脚本:
输出:
Full Script:
Output:
另一种方法,使用 MethodType:
Another approach, with MethodType:
在 Python 世界中,MonkeyPatching 是不受欢迎的。
您确实应该使用 Mixin 方法并使用多重继承。
然后您可以动态替换(更新)父级以达到所需的效果。
MonkeyPatching is frowned upon, in the python world.
You should really use the Mixin approach and use Multiple inheritance.
You can then dynamically replace (update) the parents to achieve the desired effect.
使用这样的装饰器:
此打印:
如果您想了解有关装饰器的更多信息,请阅读以下内容: http:// www.drdobbs.com/web-development/184406073
以上并不是一个“好公民”装饰器,阅读文章就知道如何写一个。请务必检查装饰器库: http://pypi.python.org/pypi/decorator
华泰
Use decorators like this:
This prints:
If you want to know more about decorators read this: http://www.drdobbs.com/web-development/184406073
The above is not a "good citizen" decorator, read the article to know how to write one. Be sure to check also decorator library: http://pypi.python.org/pypi/decorator
HTH