__str__ vs. __repr__

Jonathan Giddy jon at rdt.monash.edu.au
Mon Nov 8 23:42:46 CET 1999


Charles Boncelet <boncelet at udel.edu> writes:

>On the dissenting side, I find the "L" useful (just as I find the
>difference between 1 and '1' useful.  Consider the following:

>>>> a,b = 2L, 2
>>>> a,b
>(2L, 2)
>>>> a**32
>4294967296L
>>>> b**32
>Traceback (innermost last):
>  File "<stdin>", line 1, in ?
>OverflowError: integer pow()

>Without the "L", it is not at all clear why a**32 works and b**32 fails.

Since the interpreter prints the __repr__ form of values, this would still 
work the same.  The proposal was to change the __str__ form of long integers.

In fact, your argument dissents against another idea in this thread that the
interpreter should display the __str__ values of results.

Personally, I tend to use the __repr__ form for details that the programmer 
needs to know about an object.  I use the __str__ form for objects that have 
a natural string conversion (exception classes are the main thing that 
spring to mind).

Someone using the interactive interpreter is not going to get very far
without 'import', and anyone who uses 'import' is, or is learning to be, a
programmer.  Therefore, I would say that interactive users should get the 
__repr__ form of the result.

Jon.




More information about the Python-list mailing list