Python - 有没有办法阻止我的列表类更改?

时间:2016-10-19 09:38:33

标签: python list class oop

我有一个名为Maps的班级,我从中创建了两个列表llml应该随着某些事件而改变,但lm(内存列表)应保持不变。

有没有办法阻止我的列表类更改? 告诉你!

class Maps(list):

    def createInitial(self, lvlC):
        #self.extend(mapDico[lvlC.value])

    #get (i,j) coords of "i" in l
    def findI(self):
        for ordinate, abscissa in enumerate(self):
            try:
                return (ordinate, abscissa.index("i"))
            except:
                pass

    #get number of "x" in lm
    def countX(self):
        nbX=0
        for rows in range(len(self)):
            for elements in range(len(self)):
                if self[rows][elements]=="x":
                    nbX+=1
        return nbX

    #load next map
    def nextLevel(self, nxtL):
        del self[:]
        self.extend(mapDico[nxtL])

lvlC=Counters()
lm=Maps()
lm.createInitial(lvlC)
l=Maps()
l.createInitial(lvlC)

这里启动"控制器"文件:

             if goalC.value>0 and goalC.value==lm.countX():
            lvlC.increment(1)
            #lvl.config(text=" | Level Complete ", bg="limegreen")
            l.nextLevel(lvlC.value)
            lm.nextLevel(lvlC.value)
            muvC.reinitialize()
            nbMuv.config(text="Moves: "+str(muvC.value))
            pushC.reinitialize()
            nbPush.config(text=" | Pushes: "+str(pushC.value))
            goalC.reinitialize()
            nbGoal.config(text=" | Goals: "+str(goalC.value))
            lvl.config(text=" | Current level: "+str(lvlC.value), bg="darkgray")
            print("New moves",muvC.value, "New pushes",pushC.value, "New level",lvlC.value)
            print("New countX: ", lm.countX())
            print("New goals: ", goalC.value)
            for i in l:
                print(i)
            for i in lm:
                print(i)
            can.delete("all")
            for i in r:
                for j in r:
                    time.sleep(.005)
                    can.create_rectangle(j*n, i*n, j*n+n, i*n+n, fill=fillSquare(i,j, l), outline="darkslategray")

2 个答案:

答案 0 :(得分:3)

您可能希望使用不同的类来定义此类内容。同一个类的实例同时是可变的和不可变的并不是一个好主意。

所以我建议你在MixinClass中定义所有方法,然后创建2个类,一个是list的子类(可变)另一个是tuple的子类(不可变)

答案 1 :(得分:1)

也许是那样的

class BaseMaps(list):

def createInitial(self, lvlC):
    #self.extend(mapDico[lvlC.value])

#get (i,j) coords of "i" in l
def findI(self):
    for ordinate, abscissa in enumerate(self):
        try:
            return (ordinate, abscissa.index("i"))
        except:
            pass

#get number of "x" in lm
def countX(self):
    nbX=0
    for rows in range(len(self)):
        for elements in range(len(self)):
            if self[rows][elements]=="x":
                nbX+=1
    return nbX

#load next map
def nextLevel(self, nxtL):
    del self[:]
    self.extend(mapDico[nxtL])



class Maps(BaseMaps):

    pass


class ReadOnlyMaps(BaseMaps):
    def __init__(self, other):
        self._list = other

    def __getitem__(self, index):
        return self._list[index]

    def __iter__(self):
        return iter(self._list)

    def __slice__(self, *args, **kw):
        return self._list.__slice__(*args, **kw)

    def __repr__(self):
        return repr(self._list)

    def __len__(self):
        return len(self._list)

    def NotImplemented(self, *args, **kw):
        raise ValueError("Read Only list proxy")

    append = pop = __setitem__ = __setslice__ = __delitem__ = NotImplemented