I'm trying to accomplish something with Python but I can't find info about this at all on internet so here is my problem.
Having this sample code:
a = 5
b = [a]
I need b[0] to be a pointer reference to a. Currently if I do
b[0] = 7
a.var will still be 5, and if I set value to a b[0], a keeps the same.
I'd like to know if there is any way in Python to store an Object in an array without copying this, so if I modify one I also modify another.
EDIT ONE
I want to clarify a bit better what I'm trying to accomplish so maybe I can find the real answer.
I have a custom class like
class Foo(object):
def __init__(self):
self.var = 7
I have also a list like this:
myList = [Foo()]
My real problem is here:
In some place of a code, I need to do the following assignment:
self.foo = myList[0]
Then when I try to access this foo var, I notice this is never updated from the original instance. So basically that is what I need.
Edit Two
All answers were right. Using id(var) on every step of my assignments I could verify the Object was the same all the time, so the error must be somewhere else. Thanks to everyone for all the help and I hope this post can help someone else in a future.