How do I get a reference to a KEY value of a dictionary?
ayc8NOSPAM at cornell.edu
Fri Aug 1 08:30:53 CEST 2003
Thanks, I think that is exactly what I'm looking for. I guess if you do a =
intern(a), then if a is equivalent but not identical to something in the
global string table, it will assign a to reference the object in the global
string table, and thus the old "a" value can be garbage collected.
Well, for a C/C++ programmer, they ARE references, and I can't imagine
thinking otherwise. They are implemented as references/pointers in the
interpreter. Thinking in value semantics can simplify things, but there
will always be some cases where it doesn't work.
I think my example is a good one. If you have a graph in an adjacency list
representation, you don't want the nodes to be copies of each other. They
should just point to a global list of nodes. This can definitely matter...
the worst case is when you have a complete graph, where you would have
n(n-1)/2 (or n(n-1) for a complete directed graph) node objects where you
only need n. Instead of 100,000 nodes (not an unreasonable size), you could
have 10 billion (totally unreasonable).
But it is a good question whether the garbage collection will make the
difference worth it, in most cases. I could reassign my references, but I
don't know when the old objects get deleted. That is, I can do a =
intern(a), but the old value of a could hang around in memory for who knows
how long. If anyone could shed some light on this, I would appreciate it.
"Terry Reedy" <tjreedy at udel.edu> wrote in message
news:Qf-cnYqDQejEJbSiXTWJjw at comcast.com...
> "Andy C" <andychup at yahoo.com> wrote in message
> news:645db655.0307311636.71923378 at posting.google.com...
> > I am new to python, so please bear with me if I am making some
> > conceptual error.
> > Basically I want to create a graph with an adjacency list
> > representation, but I don't want any of the adjacency lists to have
> > duplicate strings when it is avoidable. I have a function
> > that adds an edge to the graph. The arguments will be distinct
> > they are read from text files. But basically I want to use the
> > dictionary as a string pool, and if the argument string equals
> > something in the pool already, don't use the argument string, just a
> > use a reference to something in the string pool already.
> Thinking in terms of 'references' can both help and hinder. (There
> have been some long recent discussion.)
> Are you familiar with this?
> >>> help('intern')
> Help on built-in function intern:
> intern(string) -> string
> ``Intern'' the given string. This enters the string in the
> table of interned strings whose purpose is to speed up dictionary
> Return the string itself or the previously interned string object
> with the
> same value.
More information about the Python-list