2016-12-29 123 views
0

在程序运行时,我会如何向列表中添加附加的locationx和位置变量?类的如何向类添加其他属性?

例子:

class Building: 
    'Common base class for all buildings' 

    def __init__(self, name, hp, img, locationx, locationy, height, width): 
     self.name = name 
     self.hp = hp 
     self.img = img 
     self.locationx = locationx 
     self.locationy = locationy 
     self.height = height 
     self.width = width 

building_shipyard = Building("Shipyard",500,shipyard,[1000,2500,5000],[1000,2500,5000],300,300) 

例如,用户将新大楼后,我可能要self.locationx和self.locationy从[1000,2500,5000]进行更新[ 1000,2500,5000,7000。

+0

请注意,您的标题有误导之嫌。你根本没有添加新的属性。相反,你只需要将'.append()'列表。 –

+1

'building_shipyard.locationx.append(7000)' –

回答

0

刚刚接触这些属性:

... 
def place_building(): 
    global building_shipyard 
    ... 
    building_shipyard.locationx = [1000,2500,5000,7000] 
    # or 
    building_shipyard.locationx.append(7000)