lesson 39 (为什么是:NameError: name 'list1' is not defined)
以下为程序执行结果:>>> stack = Stack(5)
>>> stack.push()
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
stack.push()
File "J:/python/python exercise/39 lesson/stack class.py", line 7, in push
list1.append(self.x)
NameError: name 'list1' is not defined
>>>
以下为源程序:
class Stack:
list1 = []
def __init__(self, x):
self.x = x
def push(self):
list1.append(self.x)
def isEmpty(self):
if Stack.list1:
return True
else:
return False
def pop(self):
list1.pop(self.x)
def top(self):
return list1[-1]
def bottom(self):
return list1 没问题呀。 class Stack:
list1 = []
def __init__(self, x):
self.x = x
def push(self):
self.list1.append(self.x)
def isEmpty(self):
if Stack.list1:
return True
else:
return False
def pop(self):
list1.pop(self.x)
def top(self):
return list1[-1]
def bottom(self):
return list1
list那改成self.list 新手·ing 发表于 2017-7-13 07:08
list那改成self.list
为什么 self.list1.append(self.x)这里将,liset1 改为了self.list1。
而list1.pop(self.x)这里可以不改?
页:
[1]