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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:10:39+00:00 2026-06-14T17:10:39+00:00

Fairly new to python, very new to python classes. Question is a bit involved.

  • 0

Fairly new to python, very new to python classes. Question is a bit involved. Most appreciative of your patience:

I have a class “Star”. It is simple. Attributes x, v, and mass. Another class, Galaxy, has an attribute “stars” which is just a list of star objects:

class Galaxy:

    numstars=0.
    stars=[]

    def __init__(self,numin,xes,vees,masses):
        self.numstars=numin
        for n in range(numin):
            self.stars.append(Star(xes[n],vees[n],masses[n]))

Galaxy also has an attribute function called time_stepper. Suffice it to say time_stepper just updates all the elements of “stars” and then returns “stars”:

def time_stepper(self,dt):
    self.velstep(dt)
    self.xstep(dt)
    self.velstep(dt)
    return(self.stars)

Now, I’m trying to drive this thing and store the various updates of “stars” in a list called “history”:

gal=Galaxy(#stuff#)
history=[]
for n in range(100):
    history.append(gal.time_stepper(.1))

Finally, my question: In each iteration of this loop, the new element of “stars” is added to “history”, but … and here it is … all the previous elements of history are over-written and given the same values as the newest element of history! So what is going on? I’ve run into things about python lists that I didn’t understand before, but I thought I finally had it nailed down. Apparently not. Thanks for your help.

Addendum:
Thanks to everyone for your help. Didn’t expect that many helpful replies and especially so soon. My problem was that I was assuming these two pieces of code were essentially the same. First:

>>> a=[]
>>> b=[1,2,3]
>>> a.append(b)
>>> b=[4,5,6]
>>> a.append(b)
>>> a
[[1, 2, 3], [4, 5, 6]]

Second:

>>> a=[]
>>> b=[1,2,3]
>>> a.append(b)
>>> b[:]=(4,5,6)
>>> b
[4, 5, 6]
>>> a.append(b)
>>> a
[[4, 5, 6], [4, 5, 6]]

And whoops! They aren’t. So in code 1, I guess, b is “re-pointed” to a completely new memory location while a[0] continues to point the the old b. In the second, the memory at b is “edited” and a[0] is still pointing to that location. After the append, a[1] is also pointing to that location. Do I have it now?

I’m very new to python and am still figuring out the “pythonic” philosophy. But to me, having a reassignment of pointers done straightforwardly, but a “deep copy” done in a more complicated way is sort of backwards from the way I usually want to do things. Can anyone enlighten me? Thanks again.

  • 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-14T17:10:40+00:00Added an answer on June 14, 2026 at 5:10 pm

    I think it is worth noting that if you have more than 1 galaxy, they’ll share the same stars. This could lead you to believe that you’re overwriting your stars when you really aren’t…

    I’m guessing you would probably be better with an __init__ that looks like:

    def __init__(self,numin,xes,vees,masses):
        self.stars = []
        self.numstars = numin
        for n in range(numin):
            self.stars.append(Star(xes[n],vees[n],masses[n]))
    

    Here I’ve shifted the class attribute stars to be an instance attribute. Now each instance will have it’s own stars list instead of sharing one stars list with all of the other galaxies in the universe.

    As others have noted, your history list is going to suffer from a similar problem (You have multiple references to the same list). However, the fix really depends on what you do in self.velstep and self.xstep. If you modify the Star objects in place, then a simple (shallow) list copy won’t do you any good (e.g. gal.time_stepper(0.1)[:]). (You’ll create a new list, but it will hold the same stars which are constantly being updated). In that case, you’ll want copy.deepcopy when you append to your history list:

    history=[]
    for n in range(100):
        history.append(copy.deepcopy(gal.time_stepper(.1)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to Python and have a question regarding the following class: class
I very new to Python, and fairly new to regex. (I have no Perl
I'm fairly new to Python, and very new to Numpy. So far I have
i have a question because i am fairly new to python, socket programming and
I'm fairly new to Python and I have a question about a neat way
I am fairly new to Python, so I welcome alternative approaches. I have a
I am fairly new to programming and to python and wxpython. I have looked
I'm just learning about python. I'm fairly new. I have the following code that
I am fairly new to python. I have been asked to create a calculator
I'm fairly new to Python and programming in general. I have done a few

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.