Python 中麻烦的继承问题



我正在尝试在python中创建两种类型的堆栈(LIFO技术)。其中一个(堆栈大师类)是样板堆栈类,另一个(CountingStack 类)继承自主类,但也具有对 pop() 调用进行计数的方法。

但是,在实例化 CountingStack 类的对象时,它似乎没有继承主类中的"__stk"属性。此列表是充当堆栈的实际容器本身。

我得到的错误是:

Traceback (most recent call last):
File "main.py", line 31, in <module>
stk.pop()
File "main.py", line 24, in pop
self.__stk.pop()
AttributeError: 'CountingStack' object has no attribute '_CountingStack__stk'

我的脚本如下:

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.__pop_counter__ = 0
self.__push_counter__ = 0
def get_counter(self):
return self.__pop_counter__
def pop(self):
self.__stk.pop()
self.__pop_counter__ += 1

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

老实说,我不确定为什么脚本要寻找一个名为"_CountingStack__stk"的属性,而不是由于继承而生成的子类属性。

提前感谢!

__为前缀的名称会精确地进行名称重整,以便它们对子类中的属性不可见或被其遮蔽。 除非您有充分的理由使用它们,否则只需使用_前缀的名称来指示私有属性。

此外,不要发明你自己的dunder(__name__)名称;它们被保留给Python实现使用。

最后,CountingStack.pop必须显式返回self._stk.pop返回的值。

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._pop_counter = 0
self._push_counter = 0
def get_counter(self):
return self._pop_counter
def pop(self):
self._pop_counter += 1
return self._stk.pop()

不过,更好的是,CountingStack.pop应该在Stack.pop方面实现,它应该使用list.pop而不是使用del。同样适用于CountingStack.push(我假设您想要给定_push_counter的定义)。

class Stack:
def __init__(self):
self._stk = []
def push(self, val):
self._stk.append(val)
def pop(self):
return self._stk.pop()

class CountingStack(Stack):
def __init__(self):
super().__init__()
self._pop_counter = 0
self._push_counter = 0
def get_counter(self):
return self._pop_counter
def pop(self):
self._pop_counter += 1
return super().pop()
def push(self, val):
self._push_counter += 1
super().push(val)

最新更新