def 函数里 return的问题

下面这段代码output是100,也是正确的output ,在class CountingStack(Stack)的 def pop(self)函数里我没有
写return Stack.pop(self),      output也是出来100, 这个return statement 是不是多余的?

```python
class Stack:
    def __init__(self):
        self.__stk = []

    def push(self, val):
        self.__stk.append(val)

    def pop(self):
        val = self.__stk[-1]
        del self.__stk[-1]
        return val


class CountingStack(Stack):
    def __init__(self):
        Stack.__init__(self)
        self.__counting=0

    def get_counter(self):
        return self.__counting

    def pop(self):
        
        self.__counting+=1
        return Stack.pop(self)
    

stk = CountingStack()
for i in range(100):
    stk.push(i)
    stk.pop()
print(stk.get_counter())


```

你 stk.pop() 是直接调用, 没有接收出栈的值 return 可以不写,但 Stack.pop(self) 必须写,否则没有出栈的操作,__stk列表不会有变化
如果是print(stk.pop()) 需要接收出栈的值就要写 return 了

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632