1 回答

TA貢獻1859條經(jīng)驗 獲得超6個贊
正如@jonrsharpe 指出的那樣,在您的undo方法中,您沒有名為 的參數(shù)_last,因此您會收到self._tot -= _last.
如果從函數(shù)參數(shù)中self._tot += _new with self._tot += self._new刪除,您可能會收到錯誤消息。self
此外,python 約定:對類屬性使用下劃線,而不是對參數(shù)使用下劃線。下面的代碼在命名約定方面更好。
class PremiumCounter:
def __init__(self, tot = 0):
self._tot = tot
self._new = 0
#self._last = 0
def add(self, new = 1):
self._tot += new
self._last = new
def undo(self):
self._tot -= self._last
def get_total(self):
return self._tot
def clear_counter(self):
self._tot = 0
添加回答
舉報