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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:06:52+00:00 2026-06-14T08:06:52+00:00

I was doing some work in Python when I ran into an unexpected problem.

  • 0

I was doing some work in Python when I ran into an unexpected problem. I have a class, and two variables x and y. x and y are meant to be separate copies of the class each with their own data. So I set x equal to the class and give it some values, then do the same with y. Once I try to use that data though, I realize that I overwrote my values for x. It seems that I have not created two separate copies of the class to work with but instead two references to the same class. So here is the general example in code:

x = someClass()
x.set(2, 0)
y = someClass()
y.set(3, 0)
print(x)

The end result is that 3 is printed instead of 2. I want to create a separate “version” of the class to hold different data for each variable. Not sure as to how though. Using Python 3.3. Here is the code for the class:

class someClass:
    def __init__(self, list = [0,0,0,0,0,0,0,0,0,0]):
        self.list = list
    def __repr__(self):
        return str(self.list)
    def set(self, loc, val):
        if ((loc >= 0) & (loc <= 9)):
            self.list[loc] = val
  • 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-14T08:06:53+00:00Added an answer on June 14, 2026 at 8:06 am

    You fell into a common newbie trap for Python. Using a list as a default variable as you did with def __init__(self, list = [0,0,0,0,0,0,0,0,0,0]) means that all instances of that class share the same list instance as the value for that argument. See also "Least Astonishment" and the Mutable Default Argument

    By the way, a common idiom if you want to use a container object such as a list as a default argument, is to instead use None and check for that. For example:

    def __init__(self, values=None):
        if values is None:
            values = [0] * 10
    

    Or whatever. Not quite as clearly self-documenting as default arguments but it works.

    As a side note, don’t use list as a variable name, as it shadows the (somewhat commonly used) list builtin.

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

Sidebar

Related Questions

I'm doing exercise #9 from http://openbookproject.net/thinkcs/python/english2e/ch09.html and have ran into something that doesn't make
my Python class has some variables that require work to calculate the first time
I have been doing some work with python-couchdb and desktopcouch. In one of the
Possible Duplicate: Converting XML to JSON using Python? I'm doing some work on App
I am doing some work with Web.Routing, using it to have friendly urls and
Recently I'm doing some work with two Series in pandas: The first Series contains
I'm doing some performance-critical Python work and want to create a function that removes
I'm doing some work with the Windows registry. Depending on whether Python is running
I am working on doing some digital filter work using Python and Numpy/Scipy. I'm
I am on exercise 43 doing some self-directed work in Learn Python The Hard

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.