Getting references to obect instances into a list

P

parent.eric.3

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?

Regards,

- Eric
 
S

Simon Brunning

2008/8/27 [email protected] said:
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.

A list only *ever* contains references to objects, so there's nothing
special to do.

I do strongly recommend this article:
<http://effbot.org/zone/python-objects.htm>.
 
B

Bruno Desthuilliers

(e-mail address removed) 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.

.... 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']
 
P

parent.eric.3

Thanks for your reply Simon.

I will read the article you told me to but first, please, have a look
at this snippet:
m = [2,3,4]
p = ['a','b','c']
q = [m,p]
q [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]
del p
q [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]


How come q is not updated after I deleted p?

This is my point.
 
M

Marc 'BlackJack' Rintsch

Thanks for your reply Simon.

I will read the article you told me to but first, please, have a look at
this snippet:

Please read the article!
m = [2,3,4]
p = ['a','b','c']
q = [m,p]
q [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]
del p
q [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]

How come q is not updated after I deleted p?

Because neither the name `q` nor the list object bound to it has anything
to do with the name `p`. ``del`` does not delete objects but *names*.
Objects exist as long as there is a reference to them. You deleted the
name `p` and thus one reference to the list with the three characters but
there's still the reference from the list bound to `q` to that three
character list.

What did you expect for an "updated q" anyway?

Ciao,
Marc 'BlackJack' Rintsch
 
S

Simon Brunning

2008/8/27 [email protected] said:
I will read the article you told me to but first, please, have a look
at this snippet:
m = [2,3,4]
p = ['a','b','c']
q = [m,p]
q [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]
del p
q [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]


How come q is not updated after I deleted p?

You deleted the *name* "p". The object that it was referring too, the
list, still has a live reference - it's an element of q - so it sticks
around. It'll only go away once the last reference to it goes.

Please, read the article. ;-)
 
J

Jeremy Sanders

I will read the article you told me to but first, please, have a look
at this snippet:
m = [2,3,4]
p = ['a','b','c']
q = [m,p]
q [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]
del p
q [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]


How come q is not updated after I deleted p?

q still holds a reference to p. Maybe you are after weak references. Have a
look at the documentation for the weakref module in the standard library.
Unfortunately you cannot store weak references to lists directly:

In [5]: class foo(object):
...: def __init__(self, lst):
...: self.lst = lst
In [6]: m = foo([2,3,4])
In [7]: p = foo(['a','b','c'])
In [8]: import weakref
In [20]: q = [weakref.proxy(m), weakref.proxy(p)]
In [23]: q[0].lst, q[1].lst
Out[23]: ([2, 3, 4], ['a', 'b', 'c'])
In [24]: del p
In [27]: q[1].lst
gives a reference error
 
T

Tim Golden

[... snipped snippet plus Mark's comment ...]

I think, in short, the best thing for you
to do is to get hold of some introductory text [1]
and to try things out. If you're coming from a
straight C / C++ environment, you maybe have no
idea just how easy it is to *do* things in Python
(as opposed to theorise about how they might or
might not work).

Seriously, I've seen *so* many discussions go past
on this list, trying to explain how Python objects
work and whether they're like this or that, value,
reference, pointer, id, blah blah blah. Much easier
to get in there and try stuff out.

Good luck :)

TJG

[1] Suggestions here; different styles suit different people:

http://wiki.python.org/moin/BeginnersGuide/Programmers?highlight=(BeginnersGuide/)

http://www.awaretek.com/tutorials.html
 
P

parent.eric.3

Ok then, my mistake: I thought 'del' was deleting the object AND
releasing the name for later user. Sorry!

This is what I should have tried in the first place:
m = [2,3,4]
p = ['a','b','c']
l = [m,p]
l [[2, 3, 4], ['a', 'b', 'c']]
p.append('w')
p ['a', 'b', 'c', 'w']
l [[2, 3, 4], ['a', 'b', 'c', 'w']]


What did you expect for an "updated q" anyway?

I am about to work on a graph where all vertices are objects
containing labels (for computing shortest paths for example) and I
plan to gradually update my labels (object's member variables). I just
wanted to make sure it was possible and what was the appropriate
mechanism.

Thanks to all of you three for shaking my thoughts. I'll try not to
ask so dumb questions again

;o)

- Eric
 
T

Timothy Grant

Thanks for your reply Simon.

I will read the article you told me to but first, please, have a look
at this snippet:
m = [2,3,4]
p = ['a','b','c']
q = [m,p]
q [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]
del p
q [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]


How come q is not updated after I deleted p?

This is my point.

Because the list still has a reference to the object formerly known as p.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top