Erik Max Francis
max at alcyone.com
Sat Jul 5 08:01:25 CEST 2003
"Batista, Facundo" wrote:
> Is this OK?
> Why this different behaviour?
It's str vs. repr, and unfortunately it generates no end of confusion.
Let's make a simple object so we can clearly see the difference:
>>> class Display:
... def __str__(self): return '(str)'
... def __repr__(self): return '(repr)'
>>> d = Display()
So whether the str or repr of the object is called, we can tell the
difference. Now, consider:
>>> print d
>>> print [d]
Printing an object prints its str, but simply specifying it as the value
of an expression in the interactive interpreter prints the repr. What
you're seeing is that both the str _and_ the repr of builtin container
types print the _repr_ of their elements. I consider this something of
a wart, but the rationalization is that it might be confusing if the str
were used when using (say) list.__str__ since it might contain spaces or
>>> [' ', 'a, b', 'c', ' ']
[' ', 'a, b', 'c', ' ']
If that were the str of the elements, it might be confusing:
>>> '[%s]' % ', '.join(map(str, [' ', 'a, b', 'c', ' ']))
'[ , a, b, c, ]'
I see the reasoning, but I'm not sure the benefit of the decision
outweighs the confusion it constantly calls. (This, along with standard
floating point equality questions, are probably the most frequently
asked questions about Python.)
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ But since when can wounded eyes see / If we weren't who we were
More information about the Python-list