本文介绍了在python中,“返回的超级对象未绑定”是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据,

哪个是超级(类型)。

我想知道什么是无界的,什么时候是无界的

I am wondering what is unbounded and when is it bounded.

推荐答案

编辑:在 super 的上下文中,大部分下面是错误的。

in the context of super, much of below is wrong. See the comment by John Y.

super(Foo,a).bar 返回称为<$ c的方法。方法解析顺序(MRO)中下一个对象的$ c> bar ,在这种情况下,绑定到对象 a 的一个实例 Foo 。如果省略了 a ,则返回的方法将不受约束。方法只是对象,但可以是绑定的或未绑定的。

super(Foo, a).bar returns the method called bar from the next object in the method resolution order (the MRO), in this case bound to the object a, an instance of Foo. If a was left out, then the returned method would be unbound. Methods are just objects, but they can be bound or unbound.

未绑定的方法是不与类的实例绑定的方法。它不会接收该类的实例作为隐式第一个参数。

An unbound method is a method that is not tied to an instance of a class. It doesn't receive the instance of the class as the implicit first argument.

您仍然可以调用未绑定的方法,但是您需要显式传递该类的实例为第一个参数。

You can still call unbound methods, but you need to pass an instance of the class explicitly as the first argument.

下面给出了一个绑定和一个未绑定方法以及如何使用它们的示例。

The following gives an example of a bound and an unbound method and how to use them.

In [1]: class Foo(object):
   ...:     def bar(self):
   ...:         print self
   ...:         

In [2]: Foo.bar
Out[2]: <unbound method Foo.bar>

In [3]: a = Foo()

In [4]: a.bar
Out[4]: <bound method Foo.bar of <__main__.Foo object at 0x4433110>>

In [5]: a.bar()
<__main__.Foo object at 0x4433110>

In [6]: Foo.bar()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-bb3335dac614> in <module>()
----> 1 Foo.bar()

TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead)

In [7]: Foo.bar(a)
<__main__.Foo object at 0x4433110>

这篇关于在python中,“返回的超级对象未绑定”是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:38