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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting references to obect instances into a list
2008/8/27 [EMAIL PROTECTED] [EMAIL PROTECTED]: 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting references to obect instances into a list
[EMAIL PROTECTED] 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'] -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting references to obect instances into a list
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
Re: Getting references to obect instances into a list
On Wed, 27 Aug 2008 08:32:52 -0700, [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting references to obect instances into a list
2008/8/27 [EMAIL PROTECTED] [EMAIL PROTECTED]: 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. [EMAIL PROTECTED] http://www.brunningonline.net/simon/blog/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting references to obect instances into a list
[EMAIL PROTECTED] 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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting references to obect instances into a list
Marc 'BlackJack' Rintsch wrote: On Wed, 27 Aug 2008 08:32:52 -0700, [EMAIL PROTECTED] 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/BeginnersGuide/Programmers?highlight=(BeginnersGuide/) http://www.awaretek.com/tutorials.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting references to obect instances into a list
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting references to obect instances into a list
On Wed, Aug 27, 2008 at 8:32 AM, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: 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] -- http://mail.python.org/mailman/listinfo/python-list