python中的实例变量,受另一个实例的值的影响

时间:2013-04-26 19:32:40

标签: class python-3.x instance-variables

class State:
'defines the state of the class'

_state = []

"""pawn = 1     rook = 2    knight = 3    bishop = 4    queen = 5    king = 6    none = 7    whiteSq = 0    blackSq = 1    whiteCol = 0    blackCol = 1"""

def __init__( self, value ):
    'constructor call with value 0 to load intial pos, and with 1 to load blank board'

    if(value == 1):
        for i in range(8):
            self._state.append([])
            for j in range(8):
                self._state[i].append([])
                self._state[i][j].append( (i+j)%2 , 7 , 7 )

    elif(value == 0):

        for i in range(8):
            self._state.append([])

            if(i==0):
                self._state[i].append( [ 0 , 1 , 2] )
                self._state[i].append( [ 1 , 1 , 3] )
                self._state[i].append( [ 0 , 1 , 4] )
                self._state[i].append( [ 1 , 1 , 5] )
                self._state[i].append( [ 0 , 1 , 6] )
                self._state[i].append( [ 1 , 1 , 4] )
                self._state[i].append( [ 0 , 1 , 3] )
                self._state[i].append( [ 1 , 1 , 2] )

            elif(i==1):
                for j in range(8):
                    self._state[i].append( [(i+j)%2 , 1 , 1] )

            elif(i==6):
                for j in range(8):
                    self._state[i].append( [(i+j)%2 , 0 , 1] )

            elif(i==7):
                self._state[i].append( [ 1 , 0 , 2] )
                self._state[i].append( [ 0 , 0 , 3] )
                self._state[i].append( [ 1 , 0 , 4] )
                self._state[i].append( [ 0 , 0 , 5] )
                self._state[i].append( [ 1 , 0 , 6] )
                self._state[i].append( [ 0 , 0 , 4] )
                self._state[i].append( [ 1 , 0 , 3] )
                self._state[i].append( [ 0 , 0 , 2] )

            else:
                for j in range(8):
                    self._state[i].append( [(i+j)%2 , 7 , 7] )  

def printState(self):
    for i in range(8):
        print( self._state[i] )

我无法理解这个输出

c=State(0);c.printState()
[[0, 1, 2], [1, 1, 3], [0, 1, 4], [1, 1, 5], [0, 1, 6], [1, 1, 4], [0, 1, 3], [1, 1, 2]]
[[1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1]]
[[0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7]]
[[1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7]]
[[0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7]]
[[1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7]]
[[0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1]]
[[1, 0, 2], [0, 0, 3], [1, 0, 4], [0, 0, 5], [1, 0, 6], [0, 0, 4], [1, 0, 3], [0, 0, 2]]

>>> a=State(0);a.printState()

[[0, 1, 2], [1, 1, 3], [0, 1, 4], [1, 1, 5], [0, 1, 6], [1, 1, 4], [0, 1, 3], [1, 1, 2], [0, 1, 2], [1, 1, 3], [0, 1, 4], [1, 1, 5], [0, 1, 6], [1, 1, 4], [0, 1, 3], [1, 1, 2]]
[[1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1]]
[[0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7]]
[[1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7]]
[[0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7]]
[[1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7]]
[[0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1]]
[[1, 0, 2], [0, 0, 3], [1, 0, 4], [0, 0, 5], [1, 0, 6], [0, 0, 4], [1, 0, 3], [0, 0, 2], [1, 0, 2], [0, 0, 3], [1, 0, 4], [0, 0, 5], [1, 0, 6], [0, 0, 4], [1, 0, 3], [0, 0, 2]]

a = State(0)是否创建了该类的新实例? 如果是这样,为什么结果如此!

不是_state类的数据成员,对于每个类的实例都不同?

我做错了什么!

1 个答案:

答案 0 :(得分:0)

阅读一些文档,并弄清楚了。

在类定义下定义任何变量将使其成为“静态数据成员”,即类变量而不是实例变量。必须在构造函数中定义所有实例变量!

class Stae:

def __init__( self, value ):
        'constructor call with value 0 to load intial pos, and with 1 to load blank board'
        self._state = []