Python Object Declaration Is Not Working
I really hope this is not a repeat...if it is, I can't find an answer
anywhere, so I apologize.
Anyways, my question is, I'm writing code where, if the data I get
requires a team instead of a player, I have a class (called Team) that
holds two SinglePlayers (also a class), and then a few other attributes,
just strings. The problem is, when I iterate through my loop, reading the
xml data and filling up my "team" variable, it seems that all the info for
the SinglePlayers doesn't get reset. This is a problem, because it is is
changing that info every time I insert a new "team" into the list of
"team" objects I have. The code is long, so I'm only going to post what's
relevant.
I have only been working with python again for a few days. I have been
working the past year in java and c++ so my brain has those concepts in my
head of how variables and structures work. I know python is different, so
if someone could please clarify why this doesn't work, that would be
amazing. Thanks!
class SinglePlayer:
entry_code = ""
first_name = ""
last_name = ""
nation = ""
seed_rank_sgl = ""
seed_rank_dbl = ""
entry_rank_sgl = ""
entry_rank_dbl = ""
class Team:
top_player = SinglePlayer()
bottom_player = SinglePlayer()
entry_code = ""
seed_rank = ""
entry_rank = ""
def DoublesEvent(self, team_nodes):
#Create List to store team objects
teams_list = []
for k in range(0, team_nodes.length):
#Get the current node
teams_node = team_nodes.item(k)
team_node = team_nodes.item(k).getElementsByTagName("Player")
top_player_node = team_node.item(0)
bottom_player_node = team_node.item(1)
#Make a new team object to fill and add to teams_list
team = Team()
team.entry_code = teams_node.getAttribute("EntryCode")
#Top Player Info
team.top_player.first_name =
top_player_node.getAttribute("FirstName")
team.top_player.last_name = top_player_node.getAttribute("LastName")
team.top_player.nation = top_player_node.getAttribute("Nation")
#Bottom Player Info
team.bottom_player.first_name =
bottom_player_node.getAttribute("FirstName")
team.bottom_player.last_name =
bottom_player_node.getAttribute("LastName")
team.bottom_player.nation = bottom_player_node.getAttribute("Nation")
eam.seed_rank = self.GetSeedRank(team)
team.entry_rank = self.GetEntryRank(team)
#Add the team to the list
teams_list.append(team)
return teams_list
No comments:
Post a Comment