The rule of literal string

James Mills prologic at shortcircuit.net.au
Wed Dec 17 18:34:12 EST 2008


On Thu, Dec 18, 2008 at 9:25 AM, Chris Rebert <clp at rebertia.com> wrote:
> As I stated previously, the key rule is:
>
> eval(repr(something)) == something

This rule is only true for basic data types;

For example:

>>> eval(repr(1)) == 1
True
>>> eval(repr([1, 2, 3])) == [1, 2, 3]
True
>>> eval(repr({"a": 1, "b": 2, "c": 3})) == {"a": 1, "b": 2, "c": 3}
True
>>> eval(repr("foo")) == "foo"
True

I guess the key thing here is that the repr
implementation (__repr__) for str, int, float
list and dict return sensible represenations
that Python _can_ evaluate with eval(...)

--JamesMills



More information about the Python-list mailing list