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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:31:41+00:00 2026-06-09T08:31:41+00:00

I learned Python last year, and was just getting back into it with a

  • 0

I learned Python last year, and was just getting back into it with a new project. I’ve hit a snag right at the beginning, and after a bunch of poring through documentation I’m still stumped. I have to believe it’s something simple, but I can’t find it. Here’s the issue:

I have the following set up in a file:

class Wrapper(object):
    def __init__(self, title=None):
        self.header = Thing("HEADER")
        if title is not None:
            self.header.content.append( Thing("TITLE", content=[str(title)]) )
        self.body = Thing("BODY")
        self.content = Thing("COMPLETE", content=[self.header, self.body])

class Thing(object):
    def __init__(self, name, content=[]):
        self.name = name
        self.content = content

Then from an interactive prompt I do:

>>> import things
>>> a = things.Wrapper("This is a title")

Now, I would expect at this point that the body attribute of a would be a Thing instance with the name "BODY" and a content consisting of an empty list. What I’m surprised to find is that its content is actually a list containing the same "TITLE" instance that a.header.content holds.

>>> a.header.name
'HEADER'
>>> a.header.content
[<test.Thing object at 0xb752290c>]
>>> a.body.name
'BODY'
>>> a.body.content
[<test.Thing object at 0xb752290c>]
>>> a.body.content[0].name
'TITLE'
>>> a.body.content[0].content
['This is a title']

I can’t for the life of me figure out how a.body.content got assigned that way. Can anyone shed some light on this?

  • 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-06-09T08:31:42+00:00Added an answer on June 9, 2026 at 8:31 am

    Try this

    class Thing(object):
        def __init__(self, name, content=None):
            if content is None:
                content = []
            self.name = name
            self.content = content
    

    The issue with [] as a default is that the default parameters are only evaluated once, when the interpreter first creates the function. For an int or other immutable type this is fine, but for mutable types (lists, dictionaries, anything that can be changed in place) this causes problems. What winds up happening is that all instances of Thing share the same content.

    This way, the empty list is created each time you call the constructor, creating a new list every time.

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

Sidebar

Related Questions

I've just learned the basics of Ruby after being very happy with Python for
So I just learned about List Comprehensions in python. some of these are getting
I am new to Python as I want to expand skills that I learned
First, I'm not a python programmer. I'm an old C dog that's learned new
I started working in Python just recently and haven't fully learned all the nuts
I'm new to python and django, and when following the Django Book I learned
I have just learned about recursion in Python and have completed assignments, one of
I just saw a code snippet in Dive into Python where a function was
I just learned using MySQLDb package for python and I am familiar with fetching
Just learned Python 3 in 7 days, and I have the feeling that there's

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.