I'm a freshman in the fantastic world of python and at the moment I'm struggling with this problem... That's an example of what I've coded:
class League():
def __init__(self, teams=[]):
self.teams = teams
def initLeague(self):
for a in range(2):
self.teams.append(Team())
self.teams[a].name = "Team" + str(a)
for b in range(3):
self.teams[a].players.append(Player())
self.teams[a].players[b].name = "Name-" + str(a) + "-" + str(b)
def printLeague(self):
for team in self.teams:
print(team.name)
for player in team.players:
print(player.name)
class Team():
def __init__(self, name=None, players=[]):
self.name = name
self.players = players
class Player():
def __init__(self, name=None):
self.name = name
nba = League()
nba.initLeague()
nba.printLeague()
The output looks like that:
Team0
Name-1-0
Name-1-1
Name-1-2
None
None
None
Team1
Name-1-0
Name-1-1
Name-1-2
None
None
None
[Finished in 0.051s]
So I would like to know where do these None come from? I noticed they depend on range(n)... it's like if the 'for a' loop is repeating inside the 'for b' loop. Another problem is that the first part of the output should be:
Team0
Name-0-0
Name-0-1
Name-0-2
...
Could someone help me? Thank you!
If you replace your Team()
class to this:
class Team():
def __init__(self, name=None):
self.name = name
self.players = []
Your output will become this:
Team0
Name-0-0
Name-0-1
Name-0-2
Team1
Name-1-0
Name-1-1
Name-1-2
which I think is what you're after. See this link: "Least Astonishment" and the Mutable Default Argument to see why.
The problem is that when using your current Team()
initialiser, the players
parameter is not passed with a default value of []
as you are expecting, instead, the value of players
is persisted for each team made.
Each team has a reference to the same list, which is why the same list is printed twice.
The reason None
is printed three times is because each time a team is created, 3 more players are added, but your inner for
loop only modifies the names of the first 3 players, leaving the last 3 players untouched.