__str__ vs. __repr__

Guido van Rossum guido at cnri.reston.va.us
Thu Nov 4 22:36:44 EST 1999


Bernhard Herzog <herzog at online.de> writes:

> How about a hook function a la __import__? I.e. have a function
> __format__ [1] in __builtin__ that is bound to repr by default. Whenever
> Python prints the result of an expression in interactive mode, it calls
> __format__ with the result as parameter and expects it to return a
> string ready for printing.

Brilliant!  The default function could do all the current built-in
magic -- print nothing if it's None, assign it to builtin _, and so
on.

I wonder if it would have to have a __magic__ name?  It could be
called display and it could be documented and usable in
non-interactive programs as well.  Or am I getting carried away?
(Possibly the habit if assigning to _ and suppressing None would make
the default display() function a bit clumsy to use.)

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




More information about the Python-list mailing list