gpt4 book ai didi

python - python中的 'super object returned is unbound'是什么意思?

转载 作者:太空狗 更新时间:2023-10-29 19:29:38 25 4
gpt4 key购买 nike

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

If the second argument is omitted, the super object returned is unbound.

哪个是 super (类型)。

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

最佳答案

您问题的其他答案( answeranswer )已经解释了绑定(bind)/未绑定(bind)这两个词的含义。

So my focus is to explain only the use of an unbound proxy object returned from the super() function (i.e. in the case it was used only with 1 argument).

获取未绑定(bind)对象的原因是以后绑定(bind)它

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

super(C).__get__(c)        # where C is a class and c is an object

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

class A:
def __init__(self, name):
self.name = name
def message(self, source):
print(f"From: {source}, class: A, object: {self.name}")

class B(A):
def __init__(self, name):
self.name = name
def message(self, source):
print(f"From: {source}, class: B, object: {self.name}")

class C(B):
def __init__(self, name):
self.name = name
def message(self, source):
print(f"From: {source}, class: C, object: {self.name}")

a = A("a")
b = B("b")
c = C("c")

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

>>> super(B)                  # unbounded (note 'None')
<super: __main__.B, None>

>>> super(B).__get__(b) # bounded to object b (note address)
<super: __main__.B, <__main__.B at 0xa9bdac0>>

>>> b # note: the same address
'<__main__.B at 0xa9bdac0>

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

>>> super(B).__get__(b).message("super(B)")
From: super(B), class: A, object: b

>>> super(C).__get__(c).message("super(C)")
From: super(C), class: B, object: c

>>> super(B).__get__(c).message("super(B)")
From: super(B), class: A, object: c

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

>>> A.name = "Class A"            # Preparing for it - 
>>> B.name = "Class B" # creating some class attributes

>>> super(B).__get__(B).name # Proxy super(B) bounded to class B
'Class A'

>>> super(B).__get__(C).name # Proxy super(B) bounded to class C
'Class A'

>>> super(C).__get__(C).name # Proxy super(C) bounded to class C
'Class B'

关于python - python中的 'super object returned is unbound'是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22403897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com