eval(repr(x)) == x
Alex Martelli
aleax at aleax.it
Sun Jan 27 06:14:47 EST 2002
Oren Tirosh wrote:
> For many python builtins eval(repr(x)) == x. I find this property very
> useful for debugging and writing tests. In some cases I write my own
> classes so their repr is an expression that recreates their state.
It's sort of nice when it works, but I don't quite see how this translates
into "very useful for debugging and writing tests". Examples please?
> For some python builtins this could be true, but isn't. Is there anyone
> else who thinks it might be more consistent if this applied to more
> builtins? I'm mostly talking about objects that have a __name__ attribute
> like types, classes and functions.
>
> The __repr__ function for these types could be something like this:
>
> def __repr__(self):
> return "%s.%s" % (self.__module__, self.__name__)
I would find it somewhat of a problem not being able to tell from repr(x)
whether x is a function, a submodule of a package, a class, etc. I think
it's more important for repr(x) to identify x reasonably well (compatibly
with not taking up a thousand characters:-) rather than striving for repr
to be eval's inverse -- which it can be only up to a point (e.g. the above
repr only works after
import module
and not after
from module import function
and the like).
Alex
More information about the Python-list
mailing list