向现有对象实例添加方法
时间:2020-03-05 18:37:28 来源:igfitidea点击:
我已经读到可以在Python中向现有对象添加方法(即,不在类定义中)。
我了解这样做并不总是一件好事。但是怎么可能呢?
解决方案:
在Python中,猴子修补通常通过覆盖我们自己的类或者函数签名来起作用。以下是来自Zope Wiki的示例:
from SomeOtherProduct.SomeModule import SomeClass def speak(self): return "ook ook eee eee eee!" SomeClass.speak = speak
该代码将覆盖/创建一个在类上称为"讲话"的方法。在杰夫·阿特伍德(Jeff Atwood)最近关于猴子修补的文章中。他在C3.0中显示了一个示例,这是我在工作中使用的当前语言。
在Python中,函数和绑定方法之间存在差异。
>>> def foo(): ... print "foo" ... >>> class A: ... def bar( self ): ... print "bar" ... >>> a = A() >>> foo <function foo at 0x00A98D70> >>> a.bar <bound method A.bar of <__main__.A instance at 0x00A9BC88>> >>>
绑定方法已"绑定"(描述性强)到一个实例,并且无论何时调用该方法,该实例都将作为第一个参数传递。
但是,作为类(而不是实例)的属性的可调用对象仍未绑定,因此我们可以在需要时修改类定义:
>>> def fooFighters( self ): ... print "fooFighters" ... >>> A.fooFighters = fooFighters >>> a2 = A() >>> a2.fooFighters <bound method A.fooFighters of <__main__.A instance at 0x00A9BEB8>> >>> a2.fooFighters() fooFighters
先前定义的实例也会被更新(只要它们本身没有覆盖属性):
>>> a.fooFighters() fooFighters
当我们要将方法添加到单个实例时,就会出现问题:
>>> def barFighters( self ): ... print "barFighters" ... >>> a.barFighters = barFighters >>> a.barFighters() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: barFighters() takes exactly 1 argument (0 given)
将函数直接添加到实例时,该函数不会自动绑定:
>>> a.barFighters <function barFighters at 0x00A98EF0>
要绑定它,我们可以在类型模块中使用MethodType函数:
>>> import types >>> a.barFighters = types.MethodType( barFighters, a ) >>> a.barFighters <bound method ?.barFighters of <__main__.A instance at 0x00A9BC88>> >>> a.barFighters() barFighters
这次,该类的其他实例没有受到影响:
>>> a2.barFighters() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: A instance has no attribute 'barFighters'
通过阅读有关描述符和元类编程的信息,可以找到更多信息。
我相信我们正在寻找的是" setattr"。
使用此设置对象上的属性。
>>> def printme(s): print repr(s) >>> class A: pass >>> setattr(A,'printme',printme) >>> a = A() >>> a.printme() # s becomes the implicit 'self' variable < __ main __ . A instance at 0xABCDEFG>
Jason Pratt发表的内容是正确的。
>>> class Test(object): ... def a(self): ... pass ... >>> def b(self): ... pass ... >>> Test.b = b >>> type(b) <type 'function'> >>> type(Test.a) <type 'instancemethod'> >>> type(Test.b) <type 'instancemethod'>
如我们所见,Python认为b()与a()没有什么不同。在Python中,所有方法只是碰巧是函数的变量。
自python 2.6起不推荐使用new模块,并在3.0版中将其删除,请使用类型
参见http://docs.python.org/library/new.html
在下面的示例中,我故意从patch_me()
函数中删除了返回值。
我认为提供返回值可能会使人相信patch返回了一个新对象,但事实并非如此,它修改了传入的对象。可能这可以促进对猴子补丁的更严格的使用。
import types class A(object):#but seems to work for old style objects too pass def patch_me(target): def method(target,x): print "x=",x print "called from", target target.method = types.MethodType(method,target) #add more if needed a = A() print a #out: <__main__.A object at 0x2b73ac88bfd0> patch_me(a) #patch instance a.method(5) #out: x= 5 #out: called from <__main__.A object at 0x2b73ac88bfd0> patch_me(A) A.method(6) #can patch class too #out: x= 6 #out: called from <class '__main__.A'>