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

  • Home
  • SEARCH
  • 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 6081349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:09:55+00:00 2026-05-23T11:09:55+00:00

I am new to Python, and noticed something I believe its a bug. Edit

  • 0

I am new to Python, and noticed something I believe its a bug.

Edit 2011-09-30:
Forget it. Now I know the attributes created are static and shared between the instances.
Hope this thread helps another python newbies in the same situation as mine.

Consider the following code:

class test():

    dictionary1 = {}
    list1 = []

    def method1(self):
        self.dictionary1.update({'1': 'unique entry'})
        self.list1 = ['unique list entry']

t=test()

print 'dictionary1 value:', t.dictionary1
print 'list1 value:', t.list1
t.method1()
print 'dictionary1 new value:', t.dictionary1
print 'list1 new value:', t.list1

t2=test()
print 'dictionary1 value:', t2.dictionary1, " -- error -- I just instantiated the class. The correct value would be {}"
print 'list1 value:', t.list1
t2.method1()
print 'dictionary1 new value:', t.dictionary1
print 'list1 new value:', t.list1

Now the question:

Why in line 19 the executed code shows: {'1': 'unique entry'}. I belive it would be: {}
Note that the list has the correct value: [] (empty list in line 20)

Using Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2

Sorry not so good english. From Brazil.

Edit 2011-09-30:
Forget it. Now I know the attributes created are static and shared between the instances.
Hope this thread helps another python newbies in the same situation as mine.

  • 1 1 Answer
  • 0 Views
  • 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-23T11:09:56+00:00Added an answer on May 23, 2026 at 11:09 am

    All your instances of test class share the same dictionary and list. The correct way to initialize the members would be:

    class Test():
        def __init__(self):
            self.dictionary1 = {}
            self.list1 = []
    

    Attributes assigned directly in the class body will be evaluated once and then shared between all instances. Since the __init__ method is run once per instance, a new list and dictionary will be created for each instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to python, poking around and I noticed this: from tkinter import *
I'm trying to understand how new instances of a Python class should be created
I am new to python and I was writing something like: t = 0.
I'm new to python. I've studied C and I noticed that that the C
I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1.
I'm a new Python programmer who is having a little trouble using 'self' in
Im new two python and am trying to grow a dictionary of dictionaries. I
I am new to python and struggling to find how to control the amount
I'm relatively new to Python and am having problems programming with Scapy, the Python
I am extremely new to python, having started to learn it less than 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.