将一个类的对象添加到另一个类的对象并在Python中检查重复项

时间:2015-02-22 17:01:42

标签: python

我必须创建两个类Player和Group。 类Player具有属性名称和级别。创建Player实例后,将在0到99的范围内创建唯一标识符。

Class Group有属性名称。创建Group实例后,将在0到20的范围内创建一个唯一的groupIdentifier。在Group类中,我需要创建一个名为addPlayer的方法来添加类Player的实例,并检查要添加的实例是否重复或者不。 addPlayer方法返回添加到其中的Player的唯一标识符。我有以下代码:

class Player(object): 
    identifier =0  
    def __init__(self,name, level=""):
        self.name = name
        self.level =level
        self.identifier += 1
        if(len(self.name) < 1):
            raise ValueError("Name.__init__:  is empty")

        if(self.identifier>=99):
            raise ValueError("Identifier.__init__:  exceeds 99")

class Group(object): 
    groupIdentifier =0  
    def __init__(self,name, player=[]):

        self.name = name
        self.player = player
        self.groupIdentifier += 1
        if(len(self.name) < 1):
            raise ValueError("Name.__init__:  is empty")

        if(self.groupIdentifier>20):
            raise ValueError("GroupIdentifier.__init__:  exceeds 20")


    def addPlayer(self, myPlayer): 
        if myPlayeris None:
            raise ValueError("myPlayer:  is empty")

        for x in range(len(self.player)):
            if self.player[x]==(myPlayer):
               raise ValueError("Player:  is duplicate")

        self.participant.append(myPlayer)
        return self.player.identifier

您能否建议我在代码中进行更改以满足要求?

1 个答案:

答案 0 :(得分:1)

使用class属性为玩家(和组!)提供标识符,而不是实例属性:

class Player(object): 
    identifier = 0  
    def __init__(self,name, level=""):
        self.name = name
        self.level =level
        # identifier belong to the class not an instance of it!
        # change in Group as well!
        self.identifier == Player.identifier
        Player.identifier += 1
        if(len(self.name) < 1): # can be 'if not self.name'
            raise ValueError("Name.__init__:  is empty")

        if(self.identifier>=99):
            raise ValueError("Identifier.__init__:  exceeds 99")

覆盖__eq__中的Player,以便能够按照标识符对玩家进行比较:

class Player(object): 
    # ... init code
    def __eq__(self, other):
        if isinstance(other, Player):
            # other object is a Player instance with the same identifier
            return self.identifier == other.identifier
        return False

您可能还想在self.players中使用Group(复数)标记其为多个list个实例的Player这一事实。

现在更改Group中的玩家比较位并使用in关键字:

if myPlayer in self.players:
    raise ValueError("Player:  is duplicate")

更改后:

class Player(object): 
    identifier = 0  
    def __init__(self,name, level=""):
        self.name = name
        self.level =level
        self.identifier += Player.identifier
        Player.identifier += 1
        if not self.name:
            raise ValueError("Name.__init__:  is empty")

        if(self.identifier>=99):
            raise ValueError("Identifier.__init__:  exceeds 99")

    def __eq__(self, other):
    if isinstance(other, Player):
        # other object is a Player instance with the same identifier
        return self.identifier == other.identifier
    return False

class Group(object): 
    groupIdentifier = 0  

    def __init__(self,name, players=[]):

        self.name = name
        self.players = players
        self.groupIdentifier = Group.groupIdentifier
        Group.groupIdentifier += 1
        if not self.name:
            raise ValueError("Name.__init__:  is empty")

        if(self.groupIdentifier>20):
            raise ValueError("GroupIdentifier.__init__:  exceeds 20")


    def addPlayer(self, myPlayer): 
        if myPlayer is None:
            raise ValueError("myPlayer:  is empty")

        if myPlayer in self.players:
            raise ValueError("Player:  is duplicate")

        # removed participant, as it is unclear where it's coming from
        # if you need it - add it to __init__
        self.players.append(myPlayer)
        return myPlayer.identifier