在python中'返回的'超级对象'是什么意思?

dsp*_*pjm 10 python class super

根据http://docs.python.org/2/library/functions.html#super,

如果省略第二个参数,则返回的超级对象是未绑定的.

哪个是超级(类型).

我想知道什么是无限的,什么时候有限.

Mar*_*anD 5

您的问题的其他答案(答案答案)已经解释了“绑定/未绑定”一词的含义。

\n
\n

因此,我的重点是仅解释从函数返回的未绑定代理对象使用(即仅与 1 个参数一起使用的情况)。super()

\n
\n

获取未绑定对象的原因是为了稍后绑定它

\n

特别是,对于从函数返回的未绑定对象,super()您可以使用其方法将其绑定到适当的对象__get__(),例如

\n
super(C).__get__(c)        # where C is a class and c is an object\n
Run Code Online (Sandbox Code Playgroud)\n
\n

为了说明这一点,让我们创建 3 个依赖类和 3 个对象 - 每个类一个:

\n
class A:\n    def __init__(self, name):\n        self.name = name\n    def message(self, source):\n        print(f"From: {source}, class: A, object: {self.name}")\n\nclass B(A):\n    def __init__(self, name):\n        self.name = name\n    def message(self, source):\n        print(f"From: {source}, class: B, object: {self.name}")\n\nclass C(B):\n    def __init__(self, name):\n        self.name = name\n    def message(self, source):\n        print(f"From: {source}, class: C, object: {self.name}")\n\na = A("a")\nb = B("b")\nc = C("c")\n
Run Code Online (Sandbox Code Playgroud)\n

现在在交互式控制台中,首先是为了理解:

\n
>>> super(B)                  # unbounded (note \'None\')\n<super: __main__.B, None>\n\n>>> super(B).__get__(b)       # bounded to object b (note address)\n<super: __main__.B, <__main__.B at 0xa9bdac0>>\n\n>>> b                         # note: the same address\n\'<__main__.B at 0xa9bdac0>\n
Run Code Online (Sandbox Code Playgroud)\n

然后 \xe2\x80\x94 显示使用类/对象的不同组合的结果
\n(在我们的委托方法的例子中.message()):

\n
>>> super(B).__get__(b).message("super(B)")\nFrom: super(B), class: A, object: b\n\n>>> super(C).__get__(c).message("super(C)")\nFrom: super(C), class: B, object: c\n\n>>> super(B).__get__(c).message("super(B)")\nFrom: super(B), class: A, object: c\n
Run Code Online (Sandbox Code Playgroud)\n

最后是绑定和未绑定代理到适当类的示例:

\n
>>> A.name = "Class A"            # Preparing for it - \n>>> B.name = "Class B"            # creating some class attributes\n\n>>> super(B).__get__(B).name      # Proxy super(B) bounded to class B\n\'Class A\'\n\n>>> super(B).__get__(C).name      # Proxy super(B) bounded to class C\n\'Class A\'\n\n>>> super(C).__get__(C).name      # Proxy super(C) bounded to class C\n\'Class B\'\n
Run Code Online (Sandbox Code Playgroud)\n


Rex*_*exE -4

“Unbound”意味着它将返回类,而不是类的实例。