maybe this is a print odds

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pipehappy

    maybe this is a print odds

    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!

  • Peter Otten

    #2
    Re: maybe this is a print odds

    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

    Comment

    Working...