0

I'd like to take a list from football player (which have few attributes like name, goals, club...) and add them to their club (which is another class) but it seems that I'm missing something because the list of players of the club is changing in the loop even if it's not called (I think I'm not correctly managing the instances of the players).

So, here is the code :

clubsWithGoal = []

class Player:
nickname = ""
imageURL = ""
numberOfGoal = 0
clubId = ""

def __init__(self, nickname, imageURL, clubId, numberOfGoal = 0):
    self.nickname = nickname
    self.imageURL = imageURL
    self.clubId = clubId
    self.numberOfGoal = numberOfGoal

def __str__(self):
    return self.nickname


class Club:
Name = ""
ImageURL = u""
id = u""
numberOfGoal = 0
listOfPlayer = []

def __init__(self, id):
    del self.listOfPlayer [:]
    self.id = id
    self.getData()

def __str__(self):
    return self.Name

def getData(self):
    try:
        results = json.load(urllib.urlopen(
            "http://worldcup.kimonolabs.com/api/clubs/" + self.id + "?apikey={youwon'tseeit}"))
        self.ImageURL = results["logo"]
        self.Name = results["name"]
    except:
        print(self.id)

def addGoal(self, numberOfGoalsToAdd):
    self.numberOfGoal += numberOfGoalsToAdd

def addPlayer(self, player):
        self.listOfPlayer.append(player)
        print("added "+player.nickname+" to "+self.Name)
        self.addGoal(player.numberOfGoal)
        print("added the "+str(player.numberOfGoal)+" of "+player.nickname+" to "+self.Name)

So here are for the model class and here is the function which must sort the players and is not working:

def createAndOrderInClub(playerlist):
foundHisClub = False
for player in playerlist:
    for club in clubsWithGoal:
        # Case 1: The club already exists and the player is part of the club
        if player.clubId == club.id:
            club.addPlayer(player)
            foundHisClub = True
            break
    # Case 2: The club doesn't already exist
    if (foundHisClub == False):
        newclub = Club(player.clubId)
        newclub.addPlayer(player)
        clubsWithGoal.append(newclub)

And an example that it's changing inside the loop (I'm java developer and new to Python): J.Bausejour is a player of Wigan ...and was replaced by Alonso, I don't know why

Laurent Meyer
  • 2,766
  • 3
  • 33
  • 57

3 Answers3

1

The listOfPlayer container, as you declared it, is a "class variable" (a bit like a static class member in java), and since lists are mutable, whenever you modify this list in any instance of Club, it will change for all instances as well.

To fix it simply remove it from the class definition, and initialize the list in the __init__ function (the strings aren't problematic since they are immutable):

class Club:
    Name = ""
    ImageURL = u""
    id = u""
    numberOfGoal = 0

    def __init__(self, id):
        self.listOfPlayer = []      
        self.id = id
        self.getData()
Akos Bannerth
  • 1,964
  • 18
  • 14
  • Thank you for your answer, as I said I'm new to python and didn't very good integrated these static class variables. I upvoted you but I won't give the tick because I chose nOOb cODEr (for his demo and the second error) and because he has only 31 points. Thanks for your help – Laurent Meyer Jun 18 '14 at 19:59
1
listOfPlayer = []

This is a class attribute, meaning it's shared for all instances of your Club class. If you're coming from Java you can think of this as a static class variable. To make this list unique for each Club class, make sure you initialize it in the constructor with the self prefix.

def __init__(self, id):
    del self.listOfPlayer [:]
    self.id = id
    self.listOfPlayer = []
    self.getData()

Make sure you do the same for all the other variables you've defined at the class level:

Name = ""
ImageURL = u""
id = u""
numberOfGoal = 0

Remove these, and initialize them in the constructor using self.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • Thank you for your answer, as I said I'm new to python and didn't very good integrated these static class variables. I upvoted you but I won't give the tick because I chose nOOb cODEr (for his demo and the second error) and because he has only 31 points. Thanks for your help – Laurent Meyer Jun 18 '14 at 19:59
1

I think the problem is that the listOfPlayer variable in the Club class is declared as a static class member, and is not initialized inside the __init__ function. This demo http://dbgr.cc/R demonstrates the point.

Besides the error above, it also looks like you're not resetting the foundHisClub variable inside the loop back to False. I would instead declare the foundHisClub variable inside the first for loop:

def createAndOrderInClub(playerlist):
    for player in playerlist:
        foundHisClub = False
        for club in clubsWithGoal:
            # Case 1: The club already exists and the player is part of the club
            if player.clubId == club.id:
                club.addPlayer(player)
                foundHisClub = True
                break
        # Case 2: The club doesn't already exist
        if (foundHisClub == False):
            newclub = Club(player.clubId)
            newclub.addPlayer(player)
            clubsWithGoal.append(newclub)
nOOb cODEr
  • 236
  • 1
  • 4