newbie question

Erno Kuusela erno-news at erno.iki.fi
Wed Feb 7 23:15:21 EST 2001


In article <3A820E6B.CB29AFBC at dsuper.net>, Denis Laroche
<dlaroche at dsuper.net> writes:

| Hello Python enthusiasts,
| I just started to learn the language this week.

| Could someone tell me how to recreate a list or a dictionary object from
| its string representation obtained with repr().

you can't do that in general, but if you only put objects that
have a eval()-able string representation inside your list/dictionary,
you can use eval().

>>> l = [1, 2]
>>> eval(repr(l)) == l
1
>>> l = [sys.stdout, l.append]
>>> eval(repr(l)) == l
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<string>", line 1

    [<open file '<stdout>', mode 'w' at 804ab68>, <built-in method append of list object at 8068e38>]
     ^
SyntaxError: invalid syntax

if you want to serialize stuff, the pickle module is more suited
to that (although it can't handle files and such either).

  -- erno



More information about the Python-list mailing list