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 3391658
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:49:40+00:00 2026-05-18T03:49:40+00:00

I have the following Python code: #!/usr/bin/env python2.6 class container(object): name = ‘container’ configuration

  • 0

I have the following Python code:

#!/usr/bin/env python2.6

class container(object):
    name = 'container'
    configuration = {'var1': 'var1',
                     'var2': 'var2'}

if __name__ == "__main__":
    container1 = container()
    container2 = container()
    container2.name = 'container2'
    container2.configuration['var2'] = 'newvar2'

    print container1.name
    print container1.configuration['var2']

I expect this to print ‘container’ and ‘var2’, but for the latter it prints ‘newvar2’ instead

Why does the configuration variable point to the same dictionary for both objects? How can I fix this?

Most answers are already explaining that name and configuration are class variables. Why does the change of container2.name not influence container1.name?

  • 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-18T03:49:41+00:00Added an answer on May 18, 2026 at 3:49 am

    Because configuration is a class variable and not an instance variable. Fixing this should fix your problem.

    class container(object):
        def __init__(self):
            self.name = 'container'
    
            self.configuration = {'var1': 'var1',
                                 'var2': 'var2'}
    

    What’s going on here is that configuration ends up living in containter.__dict__ instead of in the dictionaries of its instances when you make it a class variable. This means that c.configuration is just accessing container.__dict__['configuration'] for all instances c.

    For any class variable, an assignment of the form c.foo = x, creates an entry for foo in c.__dict__ which shadows its entry in container.__dict__. So a lookup will return that first. If you delete it, then a lookup will go back to retrieving the class instance. You could do

    c = container()
    c.configuration = x
    

    and then c.configuration would be whatever x was. But inserting a key isn’t an assignment, it’s a method call on an existing object accessed through an existing binding.

    You could get away with making name a class variable but if you want it to change from one instance to another then it should be an instance variable (unless you want a class wide default of course).

    so:

    1. An assignment (using =, setattr or directly inserting in __dict__) on an instance will shadow a class variable. The class variable is still there.
    2. A lookup (calling a method, accessing the value) on an instance will grab an instance attribute if it exists and a class variable otherwise.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #!/usr/bin/env python import sys from PyQt4 import QtGui, QtCore
I have the following code as my python server: #!/usr/bin/python3 from http.server import HTTPServer,
I have opened a shelve using the following code: #!/usr/bin/python import shelve #Module:Shelve is
For the following Python 2.7 code: #!/usr/bin/python def func_a(): print "func_a" c = 0
I have a problem passing a string argument using Perl. The following code #!/usr/bin/perl
The following code doesn't output anything(why?). #!/usr/bin/python import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("www.python.org"
I have a script a.py : #!/usr/bin/env python def foo(arg1, arg2): return int(arg1) +
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
I have the following code in python from selenium import webdriver from selenium.webdriver.common.by import
So I have the following code GAE code in python and it is a

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.