[Python-Dev] The purpose of the 'repr' builtin function

Guido van Rossum guido@python.org
Tue, 11 Apr 2000 11:33:15 -0400


> Currently the wrapper classes UserList und UserString contain the
> following method:
> 
>     def __repr__(self): return repr(self.data)
> 
> I wonder about the following alternatives:
> 
>     def __repr__(self):
>         return self.__class__.__name__ + "(" + repr(self.data) + ")"

Yes and no.  It would make them behave less like their "theoretical"
base class, but you're right that it's better to be honest in repr().
Their str() could still look like self.data.

> or even more radical (here only for lists as an example):
> 
>     def __repr__(self):
>         result = [self.__class__.__name__, "("]
>         for item in self.data:
>             result.append(repr(item))
>             result.append(", ")
>         result.append(")")
>         return "".join(result)

What's the advantage of this?  It seems designed to be faster, but I
doubt that it really is -- have you timed it?  I'd go for simple --
how time-critical can repr() be...?

--Guido van Rossum (home page: http://www.python.org/~guido/)