为什么在使用 super() 时必须指定自己的类,有没有办法解决它?

发布于 2024-07-11 22:02:30 字数 327 浏览 4 评论 0原文

当使用Python的super()进行方法链接时,你必须显式指定你自己的类,例如:

class MyDecorator(Decorator):
    def decorate(self):
        super(MyDecorator, self).decorate()

我必须指定我的类的名称MyDecorator作为参数到super()。 这不是干的。 当我现在重命名我的班级时,我将不得不重命名两次。 为什么要这样实现呢? 有没有办法避免将类名写两次(或更多次)?

When using Python's super() to do method chaining, you have to explicitly specify your own class, for example:

class MyDecorator(Decorator):
    def decorate(self):
        super(MyDecorator, self).decorate()

I have to specify the name of my class MyDecorator as an argument to super(). This is not DRY. When I rename my class now I will have to rename it twice. Why is this implemented this way? And is there a way to weasel out of having to write the name of the class twice(or more)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

像你 2024-07-18 22:02:30

在Python 3.0中,您可以使用super(),它相当于super(ThisClass, self)

文档此处。 文档中的代码示例:

class C(B):
    def method(self, arg):
        super().method(arg)    
        # This does the same thing as: super(C, self).method(arg)

In Python 3.0, you can use super() which is equivalent to super(ThisClass, self).

Documentation here. Code sample from the documentation:

class C(B):
    def method(self, arg):
        super().method(arg)    
        # This does the same thing as: super(C, self).method(arg)
九歌凝 2024-07-18 22:02:30

BDFL 对此表示同意。 在 Python 3.0 中,添加了对不带参数的 super() 的支持:

PEP 3135:新super()。 现在,您可以不带参数调用 super(),并且(假设这是在 class 语句内定义的常规实例方法中)将自动选择正确的类和实例。 如果有参数,super() 的行为不会改变。

另请参阅 Python 2.6 的 Pep 367 - New Super

The BDFL agrees. In Python 3.0, support for super() with no arguments was added:

PEP 3135: New super(). You can now invoke super() without arguments and (assuming this is in a regular instance method defined inside a class statement) the right class and instance will automatically be chosen. With arguments, the behavior of super() is unchanged.

Also see Pep 367 - New Super for Python 2.6.

别理我 2024-07-18 22:02:30

这个答案是错误的,尝试一下:

def _super(cls):
    setattr(cls, '_super', lambda self: super(cls, self))
    return cls

class A(object):
    def f(self):
        print 'A.f'

@_super
class B(A):
    def f(self):
        self._super().f()

@_super
class C(B):
    def f(self):
        self._super().f()

C().f() # maximum recursion error

在Python 2中有一种使用装饰器的方法:

def _super(cls):
    setattr(cls, '_super', lambda self: super(cls, self))
    return cls

class A(object):
    def f(self):
        print 'A.f'

@_super
class B(A):
    def f(self):
        self._super().f()

B().f() # >>> A.f

This answer is wrong, try:

def _super(cls):
    setattr(cls, '_super', lambda self: super(cls, self))
    return cls

class A(object):
    def f(self):
        print 'A.f'

@_super
class B(A):
    def f(self):
        self._super().f()

@_super
class C(B):
    def f(self):
        self._super().f()

C().f() # maximum recursion error

In Python 2 there is a way using decorator:

def _super(cls):
    setattr(cls, '_super', lambda self: super(cls, self))
    return cls

class A(object):
    def f(self):
        print 'A.f'

@_super
class B(A):
    def f(self):
        self._super().f()

B().f() # >>> A.f
林空鹿饮溪 2024-07-18 22:02:30

您还可以避免在旧版本的 python 中编写具体的类名,方法是使用

def __init__(self):
    super(self.__class__, self)
    ...

you can also avoid writing a concrete class name in older versions of python by using

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