Recursive loading trouble for immutables
Mel
mwilson at the-wire.com
Sun Nov 25 07:21:41 EST 2007
rekkufa wrote:
> I am currently building a system for serializing python objects
> to a readable file-format, as well as creating python objects by
> parsing the same format. It is more or less complete except for
> a single issue I just cannot figure out by myself: How to load
> data that specifies immutables that recursively reference
> themselves.
>
> There are only a few solutions I can think of.
>
> One: While loading recursive objects, I always create empty versions
> of objects (lists, dicts, classes) etc, and fill them in afterwards.
> This works fine for loading recursive lists and such, but as
> immutables are, well, immutable, this gets me nowhere with important
> datatypes like tuples.
> [ ... ]
I can imagine a C function that might do it.
If it were a list, of course, a Python function would be easy:
def IdioList (contents, marker):
t = []
t[:] = [x if x is not marker else t for x in contents]
return t
With tuples, by the time we have the tuple's identity it's too late,
but I suspect that in C we could bend the rules enough.
The marker would be a caller-supplied object with the property that
the caller would never want to put it in a self-referencing sequence.
I'll have to check this out today to see.
Mel.
More information about the Python-list
mailing list