nested structure with "internal references"

Kent Turbo kent at comintern.ru
Fri Sep 25 21:02:16 CEST 2009


On Sep 25, 9:11 pm, Torsten Mohr <tm... at s.netic.de> wrote:
> I'd like to refer to another entry and not copy that entry, i need to
> know later that this is a reference to another entry, i need to find
> also access that entry then.
>
> The references only need to refer to entries in this structure.
> The lists may change at runtime (entries removed / added), so
> storing the index may not help.

Python already stores everything as a reference, no need to invent
your own.
Consider this example:

>>> data = "This is real data"
>>> container = [ data, [ data ]]
>>> container
['This is real data', ['This is real data']]

'container' contains a reference to the data, and another list, with
another reference. You can easily check this by using a built-in
function id() which returns an object's identity:

>>> id(container[0])
140191569359088
>>> id(container[1][0])
140191569359088
>>> id(data)
140191569359088

So all python lists and dicts hold are references, and the real data
is elsewhere. Do you really need to have real reference to your data
in one place, or you are trying to avoid infinite loops in your data
(python handles this as well)?



More information about the Python-list mailing list