about __str__

Konstantinos Pachopoulos kostaspaxos at yahoo.gr
Thu Sep 20 17:08:02 EDT 2007


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



More information about the Python-list mailing list