2013-03-17 59 views
0

我上課點我的清單發生了什麼?

class Dot: 
    def __init__(self, x, y): 
    self.x=x 
    self.y=y 

我有類簇

class Cluster: 
    ic=0 
    List=[Dot] 
    colour=0 
    def __init__(self, Dot): 
     self.List[self.ic]=Dot 
     self.ic=self.ic+1 
    def includeDot(self, Dot): 
     self.List[self.ic]=Dot 
     self.ic=self.ic+1 

其中包括點(列表)的列表。

而且我有類ClusterMaker哪裏是集羣的列表(和其他一些程序,但是這並不重要,這個問題)

class ClusterMaker: 
    total=0 
    i=0 
    CList=[Cluster] 
    def addCluster(self,Cluster): 
     self.CList.append(Cluster)  

最後,還有我的形式按鈕,開始創建點和簇

def onChoose(self):    
     # ClMaker=ClusterMaker() 
     self.total=self.ent.get().strip() #how many dots we haver 
     self.CM=ClusterMaker() 
     i=0  
     while (i < int(self.total)): 
      dot=Dot(randint(0, 575), randint(0,670)) 
      clst=Cluster(dot) 
      clst.colour= randrange(100, 999, 15) 
      self.CM.addCluster(clst) 
      box.showerror('j', str(str(self.CM.CList[i].List[0].x)+str(clst.List[0].x))) 
      this box shows us x coord of every dot in our cluster list 
      self.canvas.create_oval(clst.List[0].x, clst.List[0].y, clst.List[0].x+10, clst.List[0].y+10, fill=str("#"+str(clst.colour))) 
      i=i+1 
     b=0 
     while(b<6): 
      box.showerror('j', str(self.CM.CList[b].List[0].x)) 
      and this box shows us x coords too 
      b=b+1 

但是我的清單上發生了什麼?爲什麼當我要求第二次顯示x座標時,它顯示所有座標系中所有點的x座標相同?

+0

'ic'(在你的類)和'self.ic'是不一樣的事情,請不要命名您的變量'List'。 – 2013-03-17 13:56:00

回答

1

類屬性實例化一次並在實例之間共享。你必須在__init__創建新的列表:

def __init__(self, Dot): 
    self.List = [Dot] 
    self.List[self.ic]=Dot 
    self.ic=self.ic+1 
+0

謝謝,我希望我可以投票給你)當我會有更多的聲譽,我會的。我只有11歲,需要15歲 – Ophelia 2013-03-17 14:54:37