eval(repr(object)) hardly ever works

Tal Einat taleinat at gmail.com
Sat Sep 16 22:13:58 EDT 2006


Matthew Wilson wrote:
> I understand that idea of an object's __repr__ method is to return a
> string representation that can then be eval()'d back to life, but it
> seems to me that it doesn't always work.
>
[snip]
>
> Any thoughts?
>

This is actually an interesting issue when you're working with Python
interpreters. For example, an object with a simple repr() which is code
to create an identical object is easy to use as a key in a dictionary.

I ran into this a while back when I was tinkering with IDLE's
auto-completion module, and ended up writing some code which checks if
an object's repr() is "reversible" or not.

- Tal
reduce(lambda m,x:[m[i]+s[-1] for i,s in enumerate(sorted(m))],
       [[chr(154-ord(c)) for c in '.&-&,l.Z95193+179-']]*18)[3]




More information about the Python-list mailing list