简体   繁体   English

使用哪个:自我还是超级?

[英]Which to use: self or super?

I am wrapping up a python class deque to queue to make my code more readable: 我包装了一个python类双端队列来排队以使我的代码更具可读性:

from collections import deque

class Queue(deque):
    def enqueue(self,a):
        super(Queue, self).append(a)

    def dequeue(self,a):
        super(Queue, self).popleft(a)

My question is which one I should use here, self.append() or super(Queue, self).append(), Why? 我的问题是我应该在此处使用self.append()还是super(Queue,self).append(),为什么?

Given these two choices, you should use self.append , because your code using super is not valid Python. 给定这两个选择,您应该使用self.append ,因为使用super的代码不是有效的Python。

The correct alternate version would be super(Queue, self).append . 正确的备用版本应该是super(Queue, self).append

super() is used to call a base class method that is redefined in the derived class. super()用于调用在派生类中重新定义的基类方法。 If your class were defining append() and popleft() methods extending their behavior, it would be reasonable to use super() inside append() and popleft() . 如果您的类正在定义扩展其行为的append()popleft()方法,则在append()popleft()内部使用super()是合理的。 However, your example redefines nothing from deque , so there is no need for super() . 但是,您的示例未从deque重新定义任何内容,因此不需要super()

The following example shows when super() is used: 下面的示例显示何时使用super()

class Queue(deque):
    def append(self, a):
        # Now you explicitly call a method from base class
        # Otherwise you will make a recursive call
        super(Queue, self).append(a)
        print "Append!!!"

However, in case of multiple inheritance what super() does is more complicated than just allowing to call a method from base class. 但是,在多重继承的情况下, super()所做的事情比仅允许从基类中调用方法要复杂得多。 Detailed understanding requires understanding MRO (method resolution order). 详细了解需要了解MRO(方法解析顺序)。 As a result, even in the example above it is usually better to write: 结果,即使在上面的示例中,通常也要编写以下代码:

class Queue(deque):
    def append(self, a):
        # Now you explicitly call a method from base class
        # Otherwise you will make a recursive call
        deque.append(self, a)
        print "Append!!!"

Go for self (putting aside that your use of super is incorrect as Borealid stated). self追求(抛开Borealid的说法,您对super的使用不正确)。

However, I believe that in this case it's better to not extend deque , but rather wrap it. 但是,我相信在这种情况下,最好不要扩展deque ,而应将其包装。

from collections import deque

class Queue(object):
    def __init__(self):
        self.q = deque

    def enqueue(self, a):
        return self.q.append(a)

    def dequeue(self, a):
        return self.q.popleft(a)

Also, note the returns - in your code they are missing and you cannot get the dequeued value. 另外,请注意返回值-在您的代码中它们丢失了,并且您无法获得出列值。

Who told you it was a good idea to even think of using super instead of self ? 谁告诉过您,甚至考虑使用super代替self是个好主意? You want to affect a single instance of the Queue, not append a to the module scope (never mind the fact that super.append throws AttributeError ) 您只想影响Queue的单个实例,而不是将a追加到模块作用域(不必担心super.append抛出AttributeError的事实)

from collections import deque

class Queue(deque):
    def enqueue(self, a):
        self.append(a)

    def dequeue(self, a):
        self.popleft(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM