使用类中的列表的属性错误 - python

时间:2017-01-04 02:41:52

标签: python python-2.7 class constructor

我收到属性错误。无法理解为什么。请帮助。

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()

1 个答案:

答案 0 :(得分:5)

变化:

def _init_(self):

成:

def __init__(self):

两个前导和尾随下划线非常重要。

如果Python无法找到__init__方法,则需要object中的方法, 这当然不会添加属性A。 说到object,在Python 2中,您应该始终继承表单object

class List(object):

获得新式课程。

或者只是切换到Python 3。

相关问题