我收到属性错误。无法理解为什么。请帮助。
class List:
def _init_(self):
self.A=[]
def Input(self):
self.A.append("Sam")
a=input("Enter no.")
self.A.append(a)
print "No. added successfully"
def Output(self):
print "The list is:"
for i in range(len(self.A)):
print self.A[i]
b=List()
b.Input()
b.Output()
答案 0 :(得分:5)
变化:
def _init_(self):
成:
def __init__(self):
两个前导和尾随下划线非常重要。
如果Python无法找到__init__
方法,则需要object
中的方法,
这当然不会添加属性A
。
说到object
,在Python 2中,您应该始终继承表单object
:
class List(object):
获得新式课程。
或者只是切换到Python 3。