如何使每个对象的列表唯一,而不是引用

时间:2018-07-11 21:57:48

标签: python list oop reference

每个人,我都是python编码的初学者,但有一个问题我无法真正找到解决方案,这是我的一小段代码

#the list with all player objects

player_list=[]

#Setting Player class

class Player:

#player's attributes

    __images=[]
    __values=0

    def __init__(self,name):
        self.__name=name

    #set the attributes

    def get_property(self,image,value):
        self.__images.append(image)
        self.__values+=value

    #display the attributes

    def show_stats(self):
        print("\nPlayer:",self.__name)
        print("Points:",self.__values,"\nImages:",end=" ")
        for x in self.__images:
            print(x,end=" ")

#Setting MainMenu

def main():

#Input number of players and their names

    count=1
    players=int(input("Number of players: "))
    for i in range(players):
        print("\nPlayer",count,end=" ")
        x=Player(name=input("name: "))
        count+=1

        #append every player object created into a list

        player_list.append(x)

    #setting for each player object an image and a value

    count=1
    for x in player_list:
        print("\nPlayer",count,end=" ")
        image=input("image: ")
        print("\nPlayer",count,end=" ")
        value=int(input("Value: "))
    count+=1

    x.get_property(image,value)

    #displaying the object's images and values

    for x in player_list:
        x.show_stats()

if __name__ == "__main__":
    main()

程序被执行,最后每个玩家对象都有其自己的POINTS集,但是每个人的图像列表都是相同的。我想象这与我的__images列表有关,该列表必须被引用并且对于每个对象不是唯一的。

任何人都可以帮助我找出如何为每个对象创建唯一列表,而不是为所有对象创建一个完整列表。

非常感谢

0 个答案:

没有答案