[Python] Trouble understanding variable/memory and scope via /r/learnprogramming


[Python] Trouble understanding variable/memory and scope

I have a history in using C#. I've only recently started using Python and am constantly running into confusing mistakes when it comes to scope or just variable types (I'm so used to static types and easy intellisense from Visual Studio). To cut to the chase, I'm having a problem and I think it has to do with either the types I'm using in Python or the way I'm going about populating my class variables.

Here is the code for my function that is causing me problems:

def GetPlayersArenaStatsBySeasonID(players,seasonID): # The API for JSON retrieval requires players names as a list, not as Player data objects # If successful, a JSON record of all players records will be returned arenaServiceRecordsJSON = h.get_arena_service_records(GetPlayersUsernames(players), seasonID) index = 0 # Each "player" is a new result for result in arenaServiceRecordsJSON["Results"]: arenaPlaylistStatsJSON = result["Result"]["ArenaStats"]["ArenaPlaylistStats"] arenaPlaylistStats = {} # Create empty dictionary to store stats for p in arenaPlaylistStatsJSON: rank = hdo.Rank() # Initialize zero-valued Rank highestRank = hdo.Rank() # Initialize zero-valued Rank playlistID = p["PlaylistId"] # Gets arena playlist ID to use as a key # Get current or concluded rank information csrJSON = p["Csr"] if (csrJSON != None): rank.DesignationID = int(csrJSON["DesignationId"]) rank.CSR = int(csrJSON["Csr"]) rank.LeaderRank = csrJSON["Rank"] rank.Tier = int(csrJSON["Tier"]) # Get highest rank information highestCsrJSON = p["HighestCsr"] if (highestCsrJSON != None): highestRank.DesignationID = int(highestCsrJSON["DesignationId"]) highestRank.CSR = int(highestCsrJSON["Csr"]) highestRank.LeaderRank = highestCsrJSON["Rank"] highestRank.Tier = int(highestCsrJSON["Tier"]) # Add arenaPlaylistStat record arenaPlaylistStats[playlistID] = hdo.ArenaPlaylistStat(rank, highestRank) #Display the current players name playlistName = metaData.GetPlaylistByID(playlistID).Name print("%s %s Rank[%s %s] HighestRank[%s %s]" % (players[index].Username,playlistName, rank.DesignationID, rank.Tier, highestRank.DesignationID, highestRank.Tier)) players[index].ServiceRecord.ArenaStats[seasonID] = arenaPlaylistStats index += 1 

I should expect to be able to loop through my players that I passed to this function and see all the arenaStats for each player. My problem is that all of the records are the exact same after completion of the function. To be even more specific they are all the same as the last players Result read from the JSON file. I know in C# if I used the "new" keyword to assign my data I would be assigning a new object to store it. I feel as though my arenaPlaylistStats object is just pointing to the new data that replaces it as I iterate through my JSON data.

Why are all my players records the exact same? If I look at my last print statement that displays the current results, everything is as I would expect it to me (each users specific arenaStats). But once my loop is done and I look at what's contained in my player objects, their data should be there.

I'm finding this a bit hard to explain so please let me know if you need me to elaborate on anything specific.

Here are the class objects I am creating and referring to in this function:

class ArenaPlaylistStat(): def __init__(self, rank = Rank(), highestRank=Rank()): self.Rank = rank self.HighestRank = highestRank class ArenaStats(): def __init__(self, arenaPlaylistStats = {}): self.ArenaPlaylistStats = arenaPlaylistStats # Dictionary Key is PLAYLIST ID | Value is a ArenaPlaylistStat Object class ServiceRecord(): def __init__(self, arenaStats = {}, warzoneRecord = None, customRecord = None): self.ArenaStats = arenaStats # Dictionary Key is SEASON ID | Value is a dictionary of ArenaStats class Player(): def __init__(self, username): self.Username = username self.ServiceRecord = ServiceRecord() 

Submitted July 13, 2017 at 02:39PM by Stryker14
via reddit http://ift.tt/2tNWH5d

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s