473,575 Members | 3,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about __str__

Hi,
i have the following class:
=============== =============== =============
class CmterIDCmts:
def __init__(self,c ommiterID,commi ts):
self.commiterID _=long(commiter ID)
self.commits_=l ong(commits)
def __str__(self):
s=""
s+="<"+str(self .commiterID_)+" :"+str(self.com mits_)+">"
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.Cm terIDCmts instance at 821045869>,
<CmterIDCmts.Cm terIDCmts 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 5046
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,c ommiterID,commi ts):
self.commiterID _=long(commiter ID)
self.commits_=l ong(commits)

def __str__(self):
s=""
s+="<"+str(self .commiterID_)+" :"+str(self.com mits_)+">"
return s
def __repr__(self):
return self.__str__()
Sep 20 '07 #2
On Sep 20, 10:08 pm, Konstantinos Pachopoulos <kostaspa...@ya hoo.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,c ommiterID,commi ts):
self.commiterID _=long(commiter ID)
self.commits_=l ong(commits)

def __str__(self):
s=""
s+="<"+str(self .commiterID_)+" :"+str(self.com mits_)+">"
return s
def __str__(self):
return "<%s:%s>" % (self.commiterI D_, self.commits_)
Sep 21 '07 #4
Bruno Desthuilliers wrote:
def __str__(self):
return "<%s:%s>" % (self.commiterI D_, 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.commiterI D_, 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
18930
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it...
15
5938
by: Jim Newton | last post by:
hi all, does anyone know what print does if there is no __str__ method? i'm trying ot override the __repr__. If anyone can give me some advice it would be great to have. I have defined a lisp-like linked list class as a subclass of list. The __iter__ seems to work as i'd like, by traversing the links, and the __repr__ seems to work...
15
2926
by: Jan Danielsson | last post by:
Sorry, but I Just Don't Get It. I did search the 'net, I did read the FAQ, but I'm too dumb to understand. As far as I can gather, __str__ is just a representation of the object. For instance: class ServerConnection: def __str__(self): buf = "Server: " + self.name + "\n" buf += "Sent bytes: " + str(self.sentBytes) + "\n"
7
1904
by: Jeffrey E. Forcier | last post by:
I am attempting to write a class whose string representation changes in response to external stimuli. While that effect is obviously possible via other means, I attempted this method first and was surprised when it didn't work, so I now want to know why :) Given the following class definition: class MyClass(object): def Edit(self):
1
1476
by: Edward C. Jones | last post by:
#! /usr/bin/env python class A(list): def __init__(self, alist, n): list.__init__(self, alist) self.n = n def __str__(self): return 'AS(%s, %i)' % (list.__str__(self), self.n)
161
7774
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.. (After reading that
5
2464
by: Mike Krell | last post by:
I'm running into problems trying to override __str__ on the path class from Jason Orendorff's path module (http://www.jorendorff.com/articles/python/path/src/path.py). My first attempt to do this was as follows: ''' class NormPath(path): def __str__(self): return 'overridden __str__: ' + path.__str__(self.normpath())
7
1413
by: netimen | last post by:
I couldn't substitute __str__ method of an instance. Though I managed to substitute ordinary method of an instance: from types import MethodType class Foo(object): pass class Printer(object):
1
7868
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8143
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6515
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5338
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3797
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2286
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1382
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1107
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.