469,312 Members | 2,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

Getting references to obect instances into a list

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
Aug 27 '08 #1
9 961
2008/8/27 pa***********@gmail.com <pa***********@gmail.com>:
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>.

--
Cheers,
Simon B.
Aug 27 '08 #2
pa***********@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']

Aug 27 '08 #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.
Aug 27 '08 #4
On Wed, 27 Aug 2008 08:32:52 -0700, pa***********@gmail.com wrote:
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
Aug 27 '08 #5
2008/8/27 pa***********@gmail.com <pa***********@gmail.com>:
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. ;-)

--
Cheers,
Simon B.
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
Aug 27 '08 #6
pa***********@gmail.com wrote:
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

--
Jeremy Sanders
http://www.jeremysanders.net/
Aug 27 '08 #7
Marc 'BlackJack' Rintsch wrote:
On Wed, 27 Aug 2008 08:32:52 -0700, pa***********@gmail.com wrote:
>Thanks for your reply Simon.

I will read the article you told me to but first, please, have a look at
this snippet:
[... 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/Beginner...eginnersGuide/)

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

Aug 27 '08 #8
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
Aug 27 '08 #9
On Wed, Aug 27, 2008 at 8:32 AM, pa***********@gmail.com
<pa***********@gmail.comwrote:
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.
--
http://mail.python.org/mailman/listinfo/python-list
Because the list still has a reference to the object formerly known as p.

--
Stand Fast,
tjg. [Timothy Grant]
Aug 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Joseph Cook | last post: by
3 posts views Thread by MuZZy | last post: by
22 posts views Thread by mehdi_mousavi | last post: by
11 posts views Thread by JohnJSal | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.