Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > about __str__

Reply
Thread Tools

about __str__

 
 
Konstantinos Pachopoulos
Guest
Posts: n/a
 
      09-20-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
 
Reply With Quote
 
 
 
 
TheFlyingDutchman
Guest
Posts: n/a
 
      09-20-2007
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__()


 
Reply With Quote
 
 
 
 
Paul Hankin
Guest
Posts: n/a
 
      09-20-2007
On Sep 20, 10:08 pm, Konstantinos Pachopoulos <(E-Mail Removed)>
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

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-21-2007
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_)


 
Reply With Quote
 
Mikael Olofsson
Guest
Posts: n/a
 
      09-24-2007
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
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-24-2007
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__ !-)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: "Aliasing" an object's __str__ to a different method Jeffrey E. Forcier Python 2 07-24-2005 02:25 PM
"Aliasing" an object's __str__ to a different method Jeffrey E. Forcier Python 7 07-24-2005 12:45 AM
FAQ: __str__ vs __repr__ Jan Danielsson Python 15 06-21-2005 01:44 PM
Access to formatting controls from within __repr__ or __str__? Dan Sommers Python 3 02-19-2005 11:02 AM
how to use __str__ and __repr__? Jim Newton Python 15 06-09-2004 01:21 PM



Advertisments