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 9 1020
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. 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']
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.
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
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/ 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/
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
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
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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Joseph Cook |
last post by:
I was wondering if there was any way to do the following:
If I have a class Base, and multiple Derived classes which all have
some different...
|
by: Iver Erling Årva |
last post by:
I have the following function which I call from a <input type="button">'s onclick event:
function showHideSub(cntrl,varclass){
var myclass =...
|
by: Martin |
last post by:
I'd like to be able to get the name of an object instance from within
a call to a method of that same object. Is this at all possible?
The...
|
by: MuZZy |
last post by:
Hi,
Is it ever possible to obtain an object's current size in managed memory?
E.g. if i want to know what's the size in bytes occupied by the...
|
by: cj |
last post by:
I'm tryin to set up a sqlcommand in VB.NET that would issue the command:
insert into server1.database.owner.table select * from...
|
by: mehdi_mousavi |
last post by:
Hi folks,
Consider the following line of code:
Address addr = ei.Address;
where ei is an instance of a class that has got a property of...
|
by: JohnJSal |
last post by:
It seems like what I want to do is something that programmers deal with
everyday, but I just can't think of a way to do it. Basically, I am
writing...
|
by: Pieter |
last post by:
Hi,
In my object oriented application (VB.NET 2.0, Windows Forms), a lot of
objects are opened in different forms by a user. For instance (a...
|
by: Dean Slindee |
last post by:
I have a project with many References to other projects of mine (data access
layer projects, for instance). When I compile a data access layer...
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |