maybe this is a print odds

Peter Otten __peter__ at web.de
Sun Nov 5 19:23:31 CET 2006


pipehappy wrote:

> I come this code:
> 
> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
> (Intel)] on win
> 32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> class He(object):
> ...     def __str__(self):
> ...         return "He"
> ...
>>>> hes = [He(),He(),He()]
>>>> print hes
> [<__main__.He object at 0x00BE92D0>, <__main__.He object at
> 0x00BE9370>, <__main
> __.He object at 0x00BE93B0>]       # I expect output: [He,He,He]
>>>> print hes[0]
> He
>>>>
> 
> Do anyone know trick to overcome this or python has some proposal to
> extend print that can do iterately print? I just want to get the way of
> print which I my guess. Thanks a lot!

Lists print their items via repr(), not str():

>>> class Ho(object):
...     def __str__(self):
...             return "That's a bit early, don't you think?"
...     def __repr__(self):
...             return "Ho"
...
>>> print [Ho()] * 3
[Ho, Ho, Ho]
>>> print Ho()
That's a bit early, don't you think?

Peter



More information about the Python-list mailing list