在 Python 中使用 super() 是个好主意吗?
或者我应该只显式引用我想要调用其方法的超类?
在引用超类的构造函数时重复超类的名称似乎很脆弱,但是此页面 http://fuhm.net/ super-harmful/ 提出了一些反对使用 super() 的好论据。
Or should I just explicitly reference the superclasses whose methods I want to call?
It seems brittle to repeat the names of super classes when referencing their constructors, but this page http://fuhm.net/super-harmful/ makes some good arguments against using super().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
专家Python编程一书在第3章讨论了“超级陷阱”的主题。值得一读。 以下是该书的结论:
编辑:今天我又读了这本书的这一部分。 我将复制更多句子,因为 super 的用法很棘手:
老式。
你的子类。
The book Expert Python Programming has discussed the topic of "super pitfalls" in chapter 3. It is worth reading. Below is the book's conclusion:
Edit: Today I read this part of the book again. I'll copy some more sentences, since super usage is tricky:
old-style.
your subclass.
您可以使用 super,但正如文章所说,它有缺点。 只要了解它们,使用该功能就没有问题。 这就像人们说“使用组合,而不是继承”或“永远不要使用全局变量”。 如果该功能存在,那就有原因。 只要确保理解原因和内容并明智地使用它们即可。
You can use super, but as the article says, there are drawbacks. As long as you know them, there is no problem with using the feature. It's like people saying "use composition, not inheritance" or "never use global variables". If the feature exists, there is a reason. Just be sure to understand the why and the what and use them wisely.
super() 试图为您解决多重继承的问题; 很难复制它的语义,除非你完全确定,否则你当然不应该创建任何新的语义。
对于单一继承来说,之间确实没有区别
,
所以我想这只是品味问题。
super() tries to solve for you the problem of multiple inheritance; it's hard to replicate its semantics and you certainly shouldn't create any new semantics unless you're completely sure.
For single inheritance, there's really no difference between
and
so I guess that's just the question of taste.
我更喜欢 super() ,因为它允许您更改继承的类(例如,当您重构并添加中间类时),而无需在所有方法上更改它。
I like super() more because it allows you to change the inherited class (for example when you're refactoring and add an intermediate class) without changing it on all the methods.
人们对 super 的问题更多的是多重继承的问题。 所以责备
super
有点不公平。 如果没有super
多重继承,情况会更糟。 Michele Simionato 在他关于 super 的博客文章中很好地总结了这一点:所以主要的教训是你应该尽量避免多重继承。
为了保持一致性,我总是使用 super,即使对于单一继承来说,这并不重要(除了不必知道父类名称的小优点之外)。 在Python 3+中
super
更方便,所以肯定应该使用 super。The problem people have with
super
is more a problem of multiple inheritance. So it is a little unfair to blamesuper
. Withoutsuper
multiple inheritance is even worse. Michele Simionato nicely wrapped this up in his blog article on super:So the main lesson is that you should try to avoid multiple inheritance.
In the interest of consistency I always use super, even if for single inheritance it does not really matter (apart from the small advantage of not having to know the parent class name). In Python 3+
super
is more convenient, so there one should definitely use super.是的,您应该使用
super()
而不是其他方法。 现在,这是 Python 3 中的标准对象继承模型。只需在
__init__
方法中坚持使用关键字参数,就不会有太多问题。 此外,您还可以使用 **kwargs 来支持继承链级别中未定义的其他参数。我同意它很脆弱,但并不比使用继承类的名称更脆弱。
Yes, you should use
super()
over other methods. This is now the standard object inheritance model in Python 3.Just stick to keyword arguments in your
__init__
methods and you shouldn't have too many problems. Additionally you can use**kwargs
to support additional parameters that are not defined in levels of the inheritance chain.I agree that it is brittle, but no less so than using the name of the inherited class.