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 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 class that runs something (a runnable) at regular
I would like to create new instances of my Class called Item inside a
Hi i made a custom class where i would like to create x instances
I would like to create multiple objects/instances in a loop. how to do this?
I need to create at runtime instances of a class that uses generics, like
I am trying to create new instances of a class and I would like
I would like to be able to create a variable that holds an instance
I would like to create this shape using just css. I am pretty sure
I would like to create a c++ type that mimic the build-in type exactly.
Similar to this Java question . I would like to specify that a variable

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.