Python:绑定方法
在下面的示例中,我尝试通过 types.MethodType(...) 绑定方法对象。这似乎不起作用。有什么建议吗?提前致谢。
import types
class Base:
def payload(self, *args):
print "Base:payload"
class Drvd(Base):
def iter(self, func):
derived_func = types.MethodType(func, self, Drvd) # bind
print "drvd_func:", derived_func
derived_func() # result: calls Base:payload
# expected:calls Drvd:payload; why???
def payload(self, *args):
print "Drvd:payload"
derived = Drvd()
base_func = Base.payload
print "base_func:", base_func
derived.iter(base_func) # pass unbound method object
输出显示:
base_func:
drvd_func: <<main.Drvd 实例在 0x00B51648 处的绑定方法 Drvd.payload>>
基础:有效负载
In following example I am trying to bind a method object via types.MethodType(...). It does not seem to work. Any suggestions? Thanks in advance.
import types
class Base:
def payload(self, *args):
print "Base:payload"
class Drvd(Base):
def iter(self, func):
derived_func = types.MethodType(func, self, Drvd) # bind
print "drvd_func:", derived_func
derived_func() # result: calls Base:payload
# expected:calls Drvd:payload; why???
def payload(self, *args):
print "Drvd:payload"
derived = Drvd()
base_func = Base.payload
print "base_func:", base_func
derived.iter(base_func) # pass unbound method object
The output shows:
base_func: <unbound method Base.payload>
drvd_func: <bound method Drvd.payload of <main.Drvd instance at 0x00B51648>>
Base:payload
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您特别请求使用
Base.payload
的底层函数 (im_func
),以及Drvd< 的假
im_class
/代码>。在iter
中的现有print
之后添加:您将看到总输出为:
即,正如您所要求的,正在使用的底层代码确实是
Base.payload——这也是您在调用中观察到的内容。
从
Base.payload
没有直接的方法可以到达Drvd.payload
,除非获取名称并使用它在getattr
上执行getattr
code>self (一个Drvd
实例),或同等内容。You're specifically requesting the use of the underlying function (
im_func
) ofBase.payload
, with a fakeim_class
ofDrvd
. Add after the existingprint
initer
:and you'll see the total output as:
i.e., as you've asked, the underlying code being used is indeed
Base.payload
-- which is also what you observe in the call.From
Base.payload
there is no direct way to get toDrvd.payload
except by getting the name and using it to do agetattr
onself
(aDrvd
instance), or equivalent.因为您将 Base 的有效负载(而不是 Drvd 的)传递给了 iter!
提示:
不过,还是要感谢你创造了一个该死的嵌合体!
Because you passed Base's payload (not Drvd's) to iter!
Hint:
Kudos for creating an accursed chimera of a thing, though!
我不太明白你想做什么 - 但对我来说,它正在按我的预期工作。您正在将 Base.payload 绑定到 Drvd - 从打印输出中您可以看到它已正确绑定到您的 Drvd 实例。我不知道为什么你期望它调用 Drvd.payload - 你已经绑定了函数对象 Base.payload。它总是那个功能。
[编辑:只是补充一下 - 如果您笼统地说出您想要做什么可能会有所帮助。你现在所做的没有多大意义 - Python 具有继承性,所以如果你想重写有效负载方法,你可以这样做 - 你不需要进行任何手动绑定。]
I don't really understand what you're trying to do - but to me it's working as I'd expect. You are binding Base.payload to Drvd - from your print out you can see it is binding correctly to your Drvd instance. I'm not sure why you are then expecting it to call Drvd.payload - you have bound the function object Base.payload. It is always going to be that function.
[edit: just to add - it might be helpful if you say what your trying to do in general terms. What you're doing at the moment doesn't make much sense - Python has inheritence so if you want to override the payload method you can just do it - you don't need to do any manual binding.]