Data Representation?

John Roth newsgroups at jhrothjr.com
Sat Oct 11 19:57:15 EDT 2003


"Kris Caselden" <google at hanger.snowbird.net> wrote in message
news:abc3fdd3.0310111540.37400ed6 at posting.google.com...
> Say I have some data:
>
> >>> a=[1]
> >>> b=[2]
> >>> link=[a,b]
>
> The simplest why to write this to a file represents it as
>
> >>> print str(link)
> [[1], [2]]
>
> Unfortunately, if this is read back in via execfile(), the whole
> dynamic nature of changing 'link' by changing 'a' and 'b' is lost. Is
> there any way to write data so that the list name is written instead
> of the list's values? Essentially, '[[a], [b]]' instead of '[[1],
> [2]]'? Any help most appreciated.

Not the way you're doing it. The thing you're getting hung
up over is that what's stored in the list is the objects, not the
names that they're bound to.

The code you give, by the way, doesn't allow you to change
'link' by changing the object bound to 'a' and 'b', and in any
case doesn't change the object bound to 'link' at all. In other
words, if you say:

a = "xxx"

the list you have bound to 'link' will not be changed to
["xxx", [2]].

However, if you say:
a[0] = "xxx"

then the list you have bound to 'link' will become:

[["xxx"], [2]]

The reason is that the first simply rebinds 'a', without
affecting the list '[1]' at all, while the second changes
the first element of that list, without rebinding 'a'.

If this makes your head ache, you're in good company.
I don't know what you're trying to accomplish; possibly
if you supplied some more details we could give you
some suggestions about which way to proceed.

My suspicion is that you need to use an object of
some kind, and possibly the pickle or marshal
modules rather than simple lists.

John Roth







More information about the Python-list mailing list