about __str__

  • Thread starter Konstantinos Pachopoulos
  • Start date
K

Konstantinos Pachopoulos

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
 
T

TheFlyingDutchman

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__()
 
P

Paul Hankin

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__
 
B

Bruno Desthuilliers

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_)
 
M

Mikael Olofsson

Bruno said:
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
 
B

Bruno Desthuilliers

Mikael Olofsson a écrit :
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__ !-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top