Python实例属性无法识别

时间:2014-06-20 15:38:46

标签: python attributes instance

我在python中有以下代码:

class state:
      def _init_(self):
            self.x=list([])
            self.possibleChests=list([])
            self.visitedChests=list([])

      def checkKeys(self):
            print self.x

      def addKey(self,x):
            self.x.append(key)


current_state=state()
future_state=state()

current_state.addKey(4)

当我运行它时,我收到以下错误:

AttributeError: state instance has no attribute 'x'

为什么'x'未被识别为实例属性?

1 个答案:

答案 0 :(得分:1)

您需要__init__周围的双下划线:

def __init__(self):

否则,Python会将该函数视为普通方法,而不是__init__ special method

相关问题