Rolling a Container Into a String

David Fraser davidf at sjsoft.com
Fri Jun 25 01:17:55 EDT 2004


Terry Reedy wrote:
> "Kamilche" <klachemin at home.com> wrote in message
> news:889cbba0.0406241742.51a2980b at posting.google.com...
> 
>>I want to convert a dict into string form, then back again. After
>>discovering that eval is insecure,
> 
> 
> With arbitrary code from an arbitrary source, yes.
> If you *know* that you are eval-ing your own safe strings, then no problem.
> 
> 
>>I wrote some code to roll a Python
>>object, dict, tuple, or list into a string.
> 
> 
> repr(object) already does that for you.  Why duplicate the work?
> 
> You only need custom a eval function, which might check that string is safe
> (no function calls, no list comps) and then eval, or which might do parsing
> and construction itself.
> 
> Terry J. Reedy
> 
> 
Or use the pprint module which does nice pretty-printing

David



More information about the Python-list mailing list