about __str__

Discussion in 'Python' started by Konstantinos Pachopoulos, Sep 20, 2007.

  1. Hi,
    i have the following class:
    ===========================================
    class CmterIDCmts:
    def __init__(self,commiterID,commits):
    self.commiterID_=long(commiterID)
    self.commits_=long(commits)


    def __str__(self):
    s=""
    s+="<"+str(self.commiterID_)+":"+str(self.commits_)+">"
    return s
    ===========================================

    and then i create the following 2 objects and list:
    ===========================================
    a=CmterIDCmts(2,3)
    b=CmterIDCmts(4,5)
    print a
    print b
    c=[]
    c.append(a)
    c.append(b)
    print c
    ===========================================
    and this is what i get:
    ===========================================
    <2:3>
    <4:5>
    [<CmterIDCmts.CmterIDCmts instance at 821045869>,
    <CmterIDCmts.CmterIDCmts instance at 1735488308>]
    ===========================================

    The __str__ method of "list" doesn't seem to call the __str__ method of
    the objects....
    ie, __str__ is not equicalent to the Java toString() method... Anyway,
    how can i fix this?

    Thanks
     
    Konstantinos Pachopoulos, Sep 20, 2007
    #1
    1. Advertising

  2. I read here recently that the __str__ method of a list calls the
    __repr__ method of each of its members. So you need to add a __repr__
    method to your class:

    class CmterIDCmts:
    def __init__(self,commiterID,commits):
    self.commiterID_=long(commiterID)
    self.commits_=long(commits)

    def __str__(self):
    s=""
    s+="<"+str(self.commiterID_)+":"+str(self.commits_)+">"
    return s
    def __repr__(self):
    return self.__str__()
     
    TheFlyingDutchman, Sep 20, 2007
    #2
    1. Advertising

  3. Konstantinos Pachopoulos

    Paul Hankin Guest

    On Sep 20, 10:08 pm, Konstantinos Pachopoulos <>
    wrote:
    >
    > The __str__ method of "list" doesn't seem to call the __str__ method of
    > the objects....
    > ie, __str__ is not equicalent to the Java toString() method... Anyway,
    > how can i fix this?


    For whatever reason, __str__ of list calls repr rather than str on
    its elements.

    You can fix your code by adding __repr__ in your class:

    Class CmterIDCmts:
    def __init__ ...
    def __str__ ...

    __repr__ = __str__

    --
    Paul Hankin
     
    Paul Hankin, Sep 20, 2007
    #3
  4. Konstantinos Pachopoulos a écrit :
    > Hi,
    > i have the following class:
    > ===========================================
    > class CmterIDCmts:

    def __init__(self,commiterID,commits):
    > self.commiterID_=long(commiterID)
    > self.commits_=long(commits)
    >
    > def __str__(self):
    > s=""
    > s+="<"+str(self.commiterID_)+":"+str(self.commits_)+">"
    > return s


    def __str__(self):
    return "<%s:%s>" % (self.commiterID_, self.commits_)
     
    Bruno Desthuilliers, Sep 21, 2007
    #4
  5. Bruno Desthuilliers wrote:
    > def __str__(self):
    > return "<%s:%s>" % (self.commiterID_, self.commits_)


    I would write that in the following way:

    def __str__(self):
    return "<%(commiterID_)s:%(commits_)s>" % self.__dict__

    More explicit IMHO. And easier to maintain, especially if the string
    would contain several insertions.

    /MiO
     
    Mikael Olofsson, Sep 24, 2007
    #5
  6. Mikael Olofsson a écrit :
    > Bruno Desthuilliers wrote:
    >> def __str__(self):
    >> return "<%s:%s>" % (self.commiterID_, self.commits_)

    >
    > I would write that in the following way:
    >
    > def __str__(self):
    > return "<%(commiterID_)s:%(commits_)s>" % self.__dict__
    >
    > More explicit IMHO. And easier to maintain, especially if the string
    > would contain several insertions.


    Agreed. Well, at least until you want to access something that's not in
    the instance's __dict__ !-)
     
    Bruno Desthuilliers, Sep 24, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Newton

    how to use __str__ and __repr__?

    Jim Newton, Jun 7, 2004, in forum: Python
    Replies:
    15
    Views:
    573
    Peter Maas
    Jun 9, 2004
  2. Dan Sommers
    Replies:
    3
    Views:
    319
    Serge Orlov
    Feb 19, 2005
  3. Jan Danielsson

    FAQ: __str__ vs __repr__

    Jan Danielsson, Jun 15, 2005, in forum: Python
    Replies:
    15
    Views:
    762
    Skip Montanaro
    Jun 21, 2005
  4. Jeffrey E. Forcier
    Replies:
    7
    Views:
    329
    Christopher Subich
    Jul 24, 2005
  5. Jeffrey E. Forcier
    Replies:
    2
    Views:
    333
    Jeffrey E. Forcier
    Jul 24, 2005
Loading...

Share This Page