简体   繁体   中英

Python Appending objects to a list

Beginner with Python, need some help to understand how to manage list of objects.

I built a list of simple objects representing a coordinate

class Point:
    def __init__(self, x, y):
        self.x=0
        self.y=0

I create a empty list to store different points: combs = [] point = Point(0, 0)

then I build different points using point and ever ytime appending to the list combs

For instance:

    point.x=2
    point.y=2
    combs.append(point)
    point.x=4
    point.y=4
    combs.append(point)

I expect that combs is something like [.. 2,2 4,4] on the contrary it's [....4,4 4,4] .

It means that every time I change the instance of a point, I change all the points stored in the list with the latest value.

How can I do this?

You are working with only a single Point. Construct a second one. See commented line below.

point = Point(0, 0)
point.x=2
point.y=2
combs.append(point)

point = Point(0, 0) # add this
point.x=4
point.y=4
combs.append(point)

By the way, your __init__ ignores its parameters -- throws them away. A better version is below. We assign self.x=x to make use of the parameter. (Likewise y ).

 def __init__(self, x, y):
        self.x=x
        self.y=x

You are appending the same variable to the combine. You need to create a new Point object and initialize it with the new values.

combine = []

p = Point(2,2)

combine.append(p)

p = Point(4,4)

combine.append(p)

The thing is when you're trying to change the value of x and y, you're expecting to have a new object (like a new x and y with different values) but you aren't. What happens is I think is whenever you set point.x =4 and point.y = 4 is you're just changing the attribute x and y in your class

take a look at this link. This helped me a lot, I encountered that kind of problem or should I say similar of yours

I suggest using the copy package

https://www.programiz.com/python-programming/shallow-deep-copy

This is because Python is using reference count for the garbage collection.

In your example you create point which increments the ref count. Then you pass it to the list which increment the count. Then change the value and pass the same variable. Which increments the count once again.

Think of it more like passing a reference or memory pointer of the point variable to the list. You gave the list twice the same pointer.

So you need to create different variables or make a deep copies https://docs.python.org/3.8/library/copy.html

You need to pass one value into point

How to add an item to your list

combs = []
point = 1 # For example
combs.append(point)

Use command lines to study

Try to use BASH or CMD... Use command lines... They will have instant feedback of your code

Good place to find basic stuff

Try to see the examples on w3scholl. It is a great place. Here is the link for W3Scholl - Python - List

Understand basics first

Before you jump into classes, try to understand lists very well! You will learn more and build a solid knowledge if you take a step by step growing! Keep pushing!!!

Custom classes, unless they are built on an immutable type, are mutable. This means that you have appended a reference to your list, and that changing the value of the references will change every instance of the reference. Consider this:

class Test():
    def __init__(self, a):
        self.a = a

>>> t1 = Test(1)
>>> t1.a
1
>>> t2 = t1
>>> t2.a
1
>>> t2.a = 2
>>> t2.a
2
>>> t1.a
2

See how changing t2 also changed t1?

So you have a few options. You could create new points instead of reusing old ones, you could use a copy() method, or you could write a method into your Point class that exports something immutable, like a tuple of the (x, y) values and append that to your list, instead of appending the entire object to your list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM