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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:17:14+00:00 2026-05-18T21:17:14+00:00

I would like to create instances of a class containing a list that’s empty

  • 0

I would like to create instances of a class containing a list that’s empty by default; instead of later setting this list to the final full list I would like to successively add items to it. Here’s a piece of sample code illustrating this:

#!/usr/bin/python

class test:
    def __init__(self, lst=[], intg=0):
        self.lista   = lst
        self.integer = intg

name_dict = {}
counter   = 0

for name in ('Anne', 'Leo', 'Suzy'):
    counter += 1

    name_dict[name] = test()
    name_dict[name].integer += 1
    name_dict[name].lista.append(counter)

    print name, name_dict[name].integer, name_dict[name].lista

When I ran the above program I expected to get

Anne 1 [1]
Leo 1 [2]
Suzy 1 [3]

as I assumed lista to always be initialised to an empty list.

What I got instead was this:

Anne 1 [1]
Leo 1 [1, 2]
Suzy 1 [1, 2, 3]

If I replace self.lista = lst by self.lista = [] it works fine, just like when I add the line name_dict[name].lista = [] to the for loop.

Why is it that the contents of the previous objects’ lists are retained, yet their values of integer aren’t? I am rather new to Python, so it would be great if someone could point out to me where my thoughts/assumptions have gone astray.

Thanks a lot in advance for your replies.

  • 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-18T21:17:15+00:00Added an answer on May 18, 2026 at 9:17 pm

    It is a very bad idea to use a mutable object as a default value, as you do here:

    def __init__(self, lst=[], intg=0):
         # ...
    

    Change it to this:

    def __init__(self, lst=None, intg=0):
         if lst is None:
             lst = []
         # ...
    

    The reason that your version doesn’t work is that the empty list is created just once when the function is defined, not every time the function is called.

    In some Python implementations you can see the value of the default values of the function by inspecting the value of func_defaults:

    print test.__init__.func_defaults
    name_dict[name] = test()
    # ...
    

    Output:

    ([],)
    Anne 1 [1]
    ([1],)
    Leo 1 [1, 2]
    ([1, 2],)
    Suzy 1 [1, 2, 3] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a stored procedure in MySQL that took a list
I would like create my own collection that has all the attributes of python
I would like create a web service in ASP.Net 2.0 that will supports JSON.
I would like to create events for certain resources that are used across various
Similar to this Java question . I would like to specify that a variable
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to create a database backed interactive AJAX webapp which has a
I would like to create an SSL connection for generic TCP communication. I think
I would like to create a file format for my app like Quake, OO,
I would like to create an application wide keyboard shortcut for a Java Swing

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.