Beyond __str__ and __repr__? (Was Re: Python doesn't know how to do math?)
Huaiyu Zhu
huaiyu at gauss.almadan.ibm.com
Fri Apr 26 20:47:36 EDT 2002
Erik Max Francis <max at alcyone.com> wrote:
>Huaiyu Zhu wrote:
>
>> It appears that another display level is needed, somewhat between repr
>> and
>> str. Let's call it 'disp' here. It is intended to be used for
>> display in a
>> controlled way, esp for nested structures.
>
>Just derive a new class and override __str__ and/or __repr__ to do what
>you want.
>
>Adding _another_ stringifying builtin function doesn't seem anywhere
>near worth it.
That's fine if the objects are user defined, but not when you build up
structures from basic building blocks.
- list, tuple and dict default to repr, so __str__ is out of the picture.
- Overriding __repr__ deprives a handy means for identity-based display.
- __repr__ for floats sucks for the purpose of display.
There is no obvious way to define a stringifying function that automatically
recurses over substructures (unlike str), works on builtins, and does not
override __repr__. If we override __repr__, what is the best way to print
the identity of an *arbitrary* object (not just id)?
Huaiyu
More information about the Python-list
mailing list