Python:绑定方法

发布于 2024-08-12 01:24:10 字数 841 浏览 2 评论 0原文

在下面的示例中,我尝试通过 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: <<ma​​in.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枯叶蝶 2024-08-19 01:24:10

您特别请求使用 Base.payload 的底层函数 (im_func),以及 Drvd< 的假 im_class /代码>。在 iter 中的现有 print 之后添加:

    print "w/class:", derived_func.im_class
    print "w/func:", derived_func.im_func

您将看到总输出为:

$ python bou.py 
base_func: <unbound method Base.payload>
drvd_func: <bound method Drvd.payload of <__main__.Drvd instance at 0x24a918>>
w/class: __main__.Drvd
w/func: <unbound method Base.payload>
Base:payload

即,正如您所要求的,正在使用的底层代码确实是 Base.payload——这也是您在调用中观察到的内容。

Base.payload 没有直接的方法可以到达 Drvd.payload,除非获取名称并使用它在 getattr 上执行 getattr code>self (一个 Drvd 实例),或同等内容。

You're specifically requesting the use of the underlying function (im_func) of Base.payload, with a fake im_class of Drvd. Add after the existing print in iter:

    print "w/class:", derived_func.im_class
    print "w/func:", derived_func.im_func

and you'll see the total output as:

$ python bou.py 
base_func: <unbound method Base.payload>
drvd_func: <bound method Drvd.payload of <__main__.Drvd instance at 0x24a918>>
w/class: __main__.Drvd
w/func: <unbound method Base.payload>
Base:payload

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 to Drvd.payload except by getting the name and using it to do a getattr on self (a Drvd instance), or equivalent.

最笨的告白 2024-08-19 01:24:10

因为您将 Base 的有效负载(而不是 Drvd 的)传递给了 iter!

提示:

print "%s: Base:payload"%repr(self)

不过,还是要感谢你创造了一个该死的嵌合体!

Because you passed Base's payload (not Drvd's) to iter!

Hint:

print "%s: Base:payload"%repr(self)

Kudos for creating an accursed chimera of a thing, though!

烏雲後面有陽光 2024-08-19 01:24:10

我不太明白你想做什么 - 但对我来说,它正在按我的预期工作。您正在将 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.]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文