471,893 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,893 software developers and data experts.

about __str__

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
Sep 20 '07 #1
5 4985
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__()
Sep 20 '07 #2
On Sep 20, 10:08 pm, Konstantinos Pachopoulos <kostaspa...@yahoo.gr>
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

Sep 20 '07 #3
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_)
Sep 21 '07 #4
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
Sep 24 '07 #5
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__ !-)

Sep 24 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

220 posts views Thread by Brandon J. Van Every | last post: by
15 posts views Thread by Jim Newton | last post: by
15 posts views Thread by Jan Danielsson | last post: by
7 posts views Thread by Jeffrey E. Forcier | last post: by
1 post views Thread by Edward C. Jones | last post: by
161 posts views Thread by KraftDiner | last post: by
5 posts views Thread by Mike Krell | last post: by
7 posts views Thread by netimen | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.