Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3876126
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:20:45+00:00 2026-05-19T22:20:45+00:00

I have a class with a dictionary. I create n number instances of the

  • 0

I have a class with a dictionary.

I create n number instances of the class.

When I += values on a key in that dictionary it is reflected in every single object I have instantiated from that object.

How do I make that dictionary unique to every instantiation of that class?

Here is how I create the Object:

for num in range(0, numOfPlayers):
    listOfPlayerFleets.append(fleet.Fleet())

Here is how call the addShip Method. I have this in a for loop and have verified that the currentPlayer int is incrementing each time.

listOfPlayerFleets[currentPlayer].addShip(typeOfShip, num)

Here is the code in my fleet object below for the example.

class Fleet:
""" Stores Fleet Numbers, Represents a fleet """


   shipNamesandNumber = {}


   def addShip(self, type, numToAdd):
      self.shipNamesandNumber[ships.shipTypesDict[type]['type']] += numToAdd  

In pydev when I step through this function call every object with shipNamesandNumbers is incremented by the numToAdd.

This happens even those the Fleet objects are at different locations in memory.

Do I have to pass in a dictionary from another class? I wrote a test class just to verify this:

class Foo:
"""Testing class with a dictionary"""

myDictionary = {}

def __init__(self):
    self.myDictionary = {'first':0, 'second':0}

def addItem(self, key, numToAdd):
    self.myDictionary[key] += numToAdd

numOfFoos = 2   
listOfFoos = []

for num in range(0, numOfFoos):
    listOfFoos.append(Foo())


listOfFoos[0].addItem('first', 1)
listOfFoos[0].addItem('first', 2)
listOfFoos[1].addItem('first', 2)
print " This is the value of foo1 it should be 3"
print listOfFoos[0].myDictionary

print "This is the value of foo2 ot should be 2"
print listOfFoos[1].myDictionary

The Foo class doesn’t have the same problem as my fleet objects having all their dictionaries modified when one dictionary is modified.

So this has made me even more confused.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:20:45+00:00Added an answer on May 19, 2026 at 10:20 pm

    You’ve created shipNamesandNumber as a class attribute since it’s contained directly within the class. Each mutation, even via self, mutates the same dictionary. If you want to prevent this then you must create an instance attribute, usually in __init__():

    class Fleet:
      def __init__(self):
        self.shipNamesandNumber = {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that inherits from a generic dictionary as follows: Class myClass
I have code similar to this: class Foo { Dictionary<Type, Object> _dict; void Create(string
I have a class derived from Dictionary. I need this class to simulate a
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a class Referrals. When you create an object in the class, it
I have a class, which holds a static dictionary of all existing instances, which
Currently, I have a class that is parsing XML and sending the dictionary that
Let's assume that I have following models: class ScoutBook(models.Model): troop = models.ForeignKey('Dictionary', limit_choices_to={'type' :
In Python, I have a Graph class that has a dictionary of vertex objects.
I have a class that contains a property-value (property bag) dictionary beside normal properties.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.