Python:你如何记住“super”参数的顺序?
正如标题所说,你如何记住super
的参数顺序?我是否遗漏了某个助记符?
经过多年的 Python 编程,我仍然需要查找它:
((郑重声明,它是 super(Type, self)
)
As the title says, how do you remember the order of super
's arguments? Is there a mnemonic somewhere I've missed?
After years of Python programming, I still have to look it up :(
(for the record, it's super(Type, self)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
继承让我想到了分类层次结构。
super
的参数顺序是分层的:首先是类,然后是实例。另一个想法,受到〜unutbu的答案的启发:
Steps in build up a right
super()
call。Inheritance makes me think of a classification hierarchy. And the order of the arguments to
super
is hierarchical: first the class, then the instance.Another idea, inspired by the answer from ~unutbu:
Steps in building up a correct
super()
call.只需记住
self
是可选 -super(Type)
提供对未绑定超类方法的访问 - 并且可选参数始终位于最后。Simply remember that the
self
is optional -super(Type)
gives access to unbound superclass methods - and optional arguments always come last.我不知道。在Python 3中我们可以这样写
I don't. In Python 3 we can just write
通常,
super
用于class
定义内部。在那里,(同样典型地),super
的第一个参数应该始终是class
的名称。Typically,
super
is used inside of aclass
definition. There, (again typically), the first argument tosuper
should always be the name of theclass
.