为什么super将类名作为参数? [英] Why does super take a class name as the argument?

查看:62
本文介绍了为什么super将类名作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,


我已经在语言参考中找到了一些搜索,并且参考了super()的行为了几页

页面但是我找不到任何关于为什么超级需要班级名称作为论据的讨论。

如果这样,请随意指出谷歌的内容已经讨论过已经死了。


超级(自我).method()好像超级可以做对了

东西...


super(kls).method()也可以在你需要从类方法访问树上时使用

。也许我错过了Metaclasses的一些东西。


我问这个的原因是我发现自己诅咒超级每个我想要的时间b $ b更改我正在处理的类的名称,当其中一个是超级实施的时候(我在猜测)是绝缘的原因

从名称变化的派生类继承他们的位置。


谢谢,

克里斯

-

克里斯格林< cm*@dok.org>

是的,但是你把宇宙脱离了背景。

Good day,

I''ve done a bit of searching in the language reference and a couple
pages referring the behavior of super() but I can''t find any
discussion of why super needs the name of the class as an argument.
Feel free to point me into the bowels of google if this has been
discussed to death already.

super(self).method() seems like super could just do the right
thing...

super(kls).method() could also work if you needed access up the tree
to from a classmethod. Maybe I''m missing something with Metaclasses.

The reason I''m asking about this is I find myself cursing super every
time I want to change the name of the class I''m working on when one of
the reason''s that super was implemented (I''m guessing) was to insulate
derived classes from the name changes of where they inherited from.

Thanks,
Chris
--
Chris Green <cm*@dok.org>
"Yeah, but you''re taking the universe out of context."

推荐答案

Chris Green写道:
Chris Green wrote:
...我找不到任何关于为什么超级需要班级名称作为论据的讨论。

超级(自我).method()似乎超级可以做正确的事情......
... I can''t find any
discussion of why super needs the name of the class as an argument.

super(self).method() seems like super could just do the right
thing...



它可以只是做正确的事情,但不是作为一个功能,只作为一个

compil er magic thingamiebob。当翻译方法时,它们是普通的函数定义,并且它们将成为一种方法的类不存在。通过包含类的名称

本身,而不是(或a)超类的名称,super可以在mro中搜索

。对于班级,并在给定班级之后找到该订单中的下一个班级。 "超级"在我看来,明确更好

比隐含更多味道没有强迫你知道整个遗产

结构。


-Scott David Daniels
Sc *********** @ Acm.Org


Chris绿色< cm*@dok.org>写道:
Chris Green <cm*@dok.org> wrote:
我在语言参考和一些引用super()行为的页面上做了一些搜索,但我找不到任何
讨论为什么super需要将类的名称作为参数。

http:// www。 python.org/2.2.1/descrintro.html#mro
http://www.python.org/2.2.1/descrintro.html#cooperation

super(self).method()好像超级可以做正确的事情......

super(kls).method()也可以工作,如果你需要从类方法访问树。也许我错过了Metaclasses的东西。
I''ve done a bit of searching in the language reference and a couple
pages referring the behavior of super() but I can''t find any
discussion of why super needs the name of the class as an argument.
http://www.python.org/2.2.1/descrintro.html#mro and
http://www.python.org/2.2.1/descrintro.html#cooperation
super(self).method() seems like super could just do the right
thing...

super(kls).method() could also work if you needed access up the tree
to from a classmethod. Maybe I''m missing something with Metaclasses.




要尝试总结(并大致简化),请考虑:


A级(对象):

def方法(个体经营):

传递


B级(A):

def方法(个体经营):

super(B,self).method()


class C(A):

def方法(个体经营):

super(C,self).method()


D类(B,C):

def方法(个体经营):

super(A,self).method()


如果超级工作只是自我, b和C中的方法()s怎么样?b $ b应该知道他们应该调用A.method()(因为

isinstance(self,D))?如果在课堂上超级工作,那么

B.method()和C.method()会在从D.method调用

时调用A.method() )。因此,超级需要知道自我类别b $ b和它所使用的方法所属的类。并且

如果您认为后者对于编译器来说很容易,请考虑


def generic_method(self):

...


B.method = generic_method


-

\S - si *** @ chiark.greenend.org.uk - http://www.chaos.org.uk/~sion/

___ | 坦率地说,我对这种或那种企鹅没有任何感情

\ X / | --Arthur C. Clarke

她的怒吼女演员是什么?ddre heafdes b?ce bump bump bump



To try and summarise (and grossly simplify), consider:

class A(object):
def method(self):
pass

class B(A):
def method(self):
super(B, self).method()

class C(A):
def method(self):
super(C, self).method()

class D(B, C):
def method(self):
super(A, self).method()

If super worked just off self, how are the method()s in B and C
supposed to know that they should be calling A.method() (since
isinstance(self, D))? If super worked just off the class, both
B.method() and C.method() would call A.method() when called
from D.method(). So super needs to know both the class of self
and the class the method it is being used from belongs to. And
if you think the latter is easy for the compiler, consider

def generic_method(self):
...

B.method = generic_method

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomet se bera eadward ofdun hl?ddre heafdes b?ce bump bump bump


Chris Green写道:

.. 。
Chris Green wrote:
.. . .
我问这个问题的原因是我发现自己每次都在诅咒超级我想改变我正在研究的班级的名字其中一个原因是'超级实现(我猜)是将派生类与其继承的名称变化隔离开来。
The reason I''m asking about this is I find myself cursing super every
time I want to change the name of the class I''m working on when one of
the reason''s that super was implemented (I''m guessing) was to insulate
derived classes from the name changes of where they inherited from.




派生类*与基类的名称更改绝缘。

super中的类必须与super
$中的类相同写入b $ b电话(即超级电话周围的电话),而不是基础

级。因此,如果您更改了班级名称,则只更改该班级中存在的超级电话




当然,通过更多支持可以更轻松来自口译员

(可能是未来的版本)。另见Guido'的论文中的autosuper [1]


Shalabh


[1] http://www.python.org/2.2.3/descrintro.html



Derived classes *are* insulated from name changes of the base classes.
The class in super has to be the same as the class in which the super
call is written (i.e. the one ''around'' the super call), not a base
class. So if you change a class name, you only change the super calls
that exist within that class.

Of course, it could be easier with more support from the interpreter
(and it may be in a future version). See also autosuper in Guido''s essay [1]

Shalabh

[1] http://www.python.org/2.2.3/descrintro.html


这篇关于为什么super将类名作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆