在 Python 中使用 super() 是个好主意吗?

发布于 2024-08-01 23:51:43 字数 187 浏览 3 评论 0原文

或者我应该只显式引用我想要调用其方法的超类?

在引用超类的构造函数时重复超类的名称似乎很脆弱,但是此页面 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

白首有我共你 2024-08-08 23:51:43

专家Python编程一书在第3章讨论了“超级陷阱”的主题。值得一读。 以下是该书的结论:

Super 的用法必须一致:在类层次结构中,super 应该在任何地方或任何地方使用。 混合超级和经典调用是一种令人困惑的做法。 人们倾向于避免使用 super,因为他们的代码更加明确。

编辑:今天我又读了这本书的这一部分。 我将复制更多句子,因为 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:

Super usage has to be consistent: In a class hierarchy, super should be used everywhere or nowhere. Mixing super and classic calls is a confusing practice. People tend to avoid super, for their code to be more explicit.

Edit: Today I read this part of the book again. I'll copy some more sentences, since super usage is tricky:

  • Avoid multiple inheritance in your code.
  • Be consistent with its usage and don't mix new-style and
    old-style.
  • Check the class hierarchy before calling its methods in
    your subclass.
走走停停 2024-08-08 23:51:43

您可以使用 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.

深巷少女 2024-08-08 23:51:43

super() 试图为您解决多重继承的问题; 很难复制它的语义,除非你完全确定,否则你当然不应该创建任何新的语义。

对于单一继承来说,之间确实没有区别

class X(Y):
    def func(self):
        Y.func(self)

class X(Y):
    def func(self):
        super().func()

所以我想这只是品味问题。

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

class X(Y):
    def func(self):
        Y.func(self)

and

class X(Y):
    def func(self):
        super().func()

so I guess that's just the question of taste.

奶气 2024-08-08 23:51:43

我更喜欢 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.

最舍不得你 2024-08-08 23:51:43

人们对 super 的问题更多的是多重继承的问题。 所以责备super有点不公平。 如果没有 super 多重继承,情况会更糟。 Michele Simionato 在他关于 super 的博客文章中很好地总结了这一点:

另一方面,人们可能想知道是否
所有超级疣都不是某些的暗示
潜在的严重问题。 它可能
问题不在于
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 blame super. Without super multiple inheritance is even worse. Michele Simionato nicely wrapped this up in his blog article on super:

On the other hand, one may wonder if
all super warts aren't hints of some
serious problem underlying. It may
well be that the problem is not with
super, nor with cooperative methods:
the problem may be with multiple
inheritance itself.

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.

故事还在继续 2024-08-08 23:51:43

是的,您应该使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文