473,800 Members | 2,379 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 5055
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
19184
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 seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
15
5954
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 properly for somethings but not others. The basic idea is that a list such as is converted to ]],...
15
2940
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
1913
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
1482
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
7897
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
2471
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
1420
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):
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10033
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
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
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.