473,773 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printing list, is this a bug?

Is the different behavior between __repr__ and __str__ intentional
when it comes to printing lists? Basically I want to print out a list
with elements of my own class, but when I overwrite __str__, __str__
doesn't get called but if I overwrite __repr__, __repr__ will get
called. Is this a bug?

For example:
>>class StrElement(obje ct):
.... def __str__(self):
.... return "String Element"
....
>>a = [StrElement(), StrElement()]
print a
[<__main__.StrEl ement object at 0xb7dc05cc>, <__main__.StrEl ement
object at 0xb7dc048c>]
>>print StrElement()
String Element

But if overwrite __repr__:
>>class ReprElement(obj ect):
.... def __repr__(self):
.... return "Repr Element"
....
>>b = [ReprElement(), ReprElement()]
print b
[Repr Element, Repr Element]
>>print ReprElement()
Repr Element

May 25 '07 #1
3 1365
William Chang wrote:
Is the different behavior between __repr__ and __str__ intentional
when it comes to printing lists? Basically I want to print out a list
with elements of my own class, but when I overwrite __str__, __str__
doesn't get called but if I overwrite __repr__, __repr__ will get
called. Is this a bug?
No, it's deliberate design. The string representation of a list object, either
list.__str__() or list.__repr__() , uses the __repr__() of its contained objects.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

May 25 '07 #2
oh okay. thanks.

May 26 '07 #3
On May 25, 3:55 pm, William Chang <mr.williamch.. .@gmail.comwrot e:
Is the different behavior between __repr__ and __str__ intentional
when it comes to printing lists? Basically I want to print out a list
with elements of my own class, but when I overwrite __str__, __str__
doesn't get called but if I overwrite __repr__, __repr__ will get
called. Is this a bug?
...
As has been mentioned, this was a deliberate design decision. Partly
to make things like str(['a', 'b, c']) less confusing.

May 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4858
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been selected. For example, if I were to have 5 pages with every second page printing, I would get the following results: Page 1 = Print OK Page 2 = Blank Page 3 = Print OK Page 4 = Blank
0
1498
by: JF Turcotte | last post by:
Hi I'm unsuccessfully trying to print a form's image under VB.NET. To print under .NET is a real pain in the , I find it to be complex, lenghty, confusing, upsetting and ultimately not to be working at all. My strategy is first to save the form's image as a bitmap (this works, I can save the image to a file). Then I use the printer's Graphics object's drawImage method to send it
1
2089
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object variable or With block variable not set. I am trying to print the contents of a list box on another form. This is the sub, and the highlighted line is where it is falling over.
0
1296
by: ALMA_J_III | last post by:
Hi all! Customer will use HP LaserJet 4200TN printer for printing of invoices. 3 different versions of invoice on different color of paper should be to print. HP LaserJet 4200TN have 3 paper source trays. Maybe some from you have experience in printing to different trays of this printer. Crystalreport Report object has PrintOptions class and the next papersource properties:...
6
482
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having trouble getting it to print on multiple pages. The second page will not print at all. Thanks in advance
0
1026
by: Galen Somerville | last post by:
In VB6 (converted to VB2005) there are 6 different sub-menu items where the user has a choice of listing items on the screen or printing out a list of items. Each such menu item puts the name of a file in a string variable and calls a print routine. Obviously I have boucoup errors and warnings. But the documentation always shows doing the printing from a button click. I don't have a button to click !!
0
1694
by: Hank | last post by:
We have been printing pdf files through Adobe, from Access for several years. Adobe version 5.0 is currently installed. Recently we have received PDF files that were created under the Adobe 7.0 version. When trying to view a file manually Adobe 5.0 displays a pop up message: "This file may contain newer information than this viewer can support. It may not open or display correctly". There is a check box with the pop-up that says,...
1
1894
by: belinda | last post by:
I have used the ff command but only get the date printout and have no clue whats wrong: 'Printing function ' - opening a Word template with bookmarks ' - read bookmarks list from template and match with the field names in Outlook form ' - value in form is that populated to the Word doc for auto-printing ' - prompt user for closing Word when printing is done. ' - Save word as C:\deletme.doc before exiting Word. ' 8/21/98 Leng Ho '...
2
9976
by: Brad Pears | last post by:
I have a vb.net 2005 application and am using the print preview screen. This screen has a printer icon on it that the user can use to print the document currently being viewed. It uses the default printer settings to print. I wanted the print preview to appear the same for all users (i.e. a default page size of 8.5x14 (legal) and portrait mode). Many users have different printers as their default (plotters etc..) and I found that various...
4
16824
ADezii
by: ADezii | last post by:
Recently, there seems to be several questions specifically related to Printers and changing Printing characteristics for Forms and Reports. For this reason alone, I decided to dedicate this week's Tip to these Topics. The Tip will actually consist of several Tips which I feel are very useful for all Users, from Newbies to Experts. In order to utilize the code contained within these Tips, you must have Access 2002 or later. How can I...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10264
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...
1
10039
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7463
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
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...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
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
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
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.