nested structure with "internal references"
J Kenneth King
james at agentultra.com
Mon Sep 28 15:36:34 CEST 2009
Hendrik van Rooyen <hendrik at microcorp.co.za> writes:
> On Friday, 25 September 2009 19:11:06 Torsten Mohr wrote:
>> I'd like to use a nested structure in memory that consists
>> of dict()s and list()s, list entries can be dict()s, other list()s,
>> dict entries can be list()s or other dict()s.
>> The lists and dicts can also contain int, float, string, ...
> This sounds terribly convoluted.
> What are you trying to do?
>> But i'd also like to have something like a "reference" to another
> Everything and its brother in Python is an object, and things like lists and
> dicts reference objects automagically.
>> 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.
> Depending on how I read this, it is either trivial or impossible:
> A name is bound to an object by assignment.
> An object can have many names bound to it.
> A name can, at different times, refer to different objects.
> An object does not know which names are bound to it, or in what sequence it
> was done.
> So you can go from name to object, but not the other way around.
> You can use the same name in a loop to refer to different objects, and in each
> iteration, use the name to store a reference to the current object in a list
> or dict.
>> Is something like this possible in Python?
> Not too sure what you are trying to do.
>> 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.
> You could start off with a list of lists of lists, to any level of nesting
> that you want. This will give you a structure like a tree with branches and
> twigs and leaves, but I am not sure if this is what you want or need, or if a
> flat structure like third normal form would suffice, or if you need a
> relational database.
> - Hendrik
I'm not really sure what he wants either, but it sounds suspiciously
like a linked list.
In regards to the OP, though not a true linked-list, Python objects can
be built with classes that describe essentially the same functionality.
Just be careful about keeping references to large in-memory objects (see
weakref) and try to avoid circular references in your mappings.
More information about the Python-list