Getting references to obect instances into a list
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Wed Aug 27 11:30:23 EDT 2008
parent.eric.3 at gmail.com a écrit :
> Hi,
>
> I would like to get the references to objets to put in a huge data
> structure (like a list or a heap for example). My objective is to use
> as less memory as possible as I have to manage huge amount of entries
> in my data structure and need to use the same elsewhere.
>
> If I were coding in C++, it would be natural to do so but as I am a
> newby to Python, I don't know yet how to achieve that.
>
> Can anyone help me with that?
Easily : in Python, you only have objects references.
>>> class Foo(object):
... def __init__(self, bar):
... self.bar = bar
...
>>> foo = Foo(42)
>>> baaz = foo
>>> blist = [foo, baaz]
>>> foo is baaz
True
>>> blist[0] is blist[1]
True
>>> blist[0] is foo
True
>>> blist[0] is baaz
True
>>> baaz.bar = "yadda"
>>> foo.bar
'yadda'
>>> [o.bar for o in blist]
['yadda', 'yadda']
More information about the Python-list
mailing list