473,800 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with __str__ if baseclass is list

#! /usr/bin/env python

class A(list):
def __init__(self, alist, n):
list.__init__(s elf, alist)
self.n = n

def __str__(self):
return 'AS(%s, %i)' % (list.__str__(s elf), self.n)

def __repr__(self):
return 'AR(%s, %i)' % (list.__repr__( self), self.n)

a = A(['x', 'y'], 7)

print 1, a
print 2, repr(a)
print 3, list.__str__(a)
print 4, list.__repr__(a )

"""
The output is:

1 AS(AR(['x', 'y'], 7), 7)
2 AR(['x', 'y'], 7)
3 AR(['x', 'y'], 7)
4 ['x', 'y']

Why is list.__str__(a) == "AR(['x', 'y'], 7)"?

Note: The problem goes away if "list.__str__(a )" is replaced with
"list.__repr__( self)".
"""
Nov 22 '05 #1
1 1482
Edward C. Jones wrote:
#! /usr/bin/env python

class A(list):
def __init__(self, alist, n):
list.__init__(s elf, alist)
self.n = n

def __str__(self):
return 'AS(%s, %i)' % (list.__str__(s elf), self.n)

def __repr__(self):
return 'AR(%s, %i)' % (list.__repr__( self), self.n)

a = A(['x', 'y'], 7)

print 1, a
print 2, repr(a)
print 3, list.__str__(a)
print 4, list.__repr__(a )

"""
The output is:

1 AS(AR(['x', 'y'], 7), 7)
2 AR(['x', 'y'], 7)
3 AR(['x', 'y'], 7)
4 ['x', 'y']

Why is list.__str__(a) == "AR(['x', 'y'], 7)"?
Because it's coded like this:
def __str__(self):
return repr(self)

That implies str(x) == repr(x), since you don't want that, don't call
list.__str__

Note: The problem goes away if "list.__str__(a )" is replaced with
"list.__repr__( self)".
"""


That's right. You *cannot* call list.__str__ because it contradicts
design of class A

Nov 22 '05 #2

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

Similar topics

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 ]],...
1
2426
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using either the list.extend method or even by having the subclass create a whole new list in the variable. The following example illustrates the situation.
7
1042
by: M O J O | last post by:
Hi, I have a baseclass and several clases that inhereits from this baseclass. In the inherited class I need to change a variable that is used in the base class, but I cant figure out how. Here's an example.... Public Class MyBaseClass Friend Shared _tableName As String
9
2964
by: MSDNAndi | last post by:
Hi, I have the following problem with VS 2005 (Professional) and C# 2.0 I will use assembly and namespace synonymously here. I have a class in namespace InheritClass inherit from a baseclass in namespace BaseClass. Then I have a class in namespace InheritClassUser that uses the class in namespace InheritClass. The references/using are from InheritClass to BaseClass and from
1
1253
by: Ilias Lazaridis | last post by:
I have some python code which looks similar to this: class Car(BaseClass) : manufacturer = factory.string() model = factory.string() modelYear = factory.integer() def __str__(self): return '%s %s %s' % (self.modelYear, self.manufacturer, self.model)
3
4450
by: skydivergm | last post by:
Hi, Consider the following code: --- template <class T> class TMyCWndWrapper : public T { public:
3
1549
by: Jonas Huckestein | last post by:
Hello, I have the following problem: I have one BaseClass, which defines the virtual functions getValue(int index) and virtually overloads the operators *, /, -, +. Now I want two classes SubClass1 and SubClass2 which extend the BaseClass and implement the virtual functions, so that I can go like this: BaseClass *oskar = new SubClass1; BaseClass *newton = new SubClass2; BaseClass *pete = oskar*newton+newton/oskar;
3
1389
by: Juha Nieminen | last post by:
This is a simplified version of the situation (the actual situation is quite more complex, but when stripped to the bare minimum, it's like this): class BaseClass { public: virtual void cleanup(); virtual ~BaseClass() { cleanup(); } };
5
5055
by: Konstantinos Pachopoulos | last post by:
Hi, i have the following class: =========================================== class CmterIDCmts: def __init__(self,commiterID,commits): self.commiterID_=long(commiterID) self.commits_=long(commits) def __str__(self):
0
9690
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
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...
1
10253
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,...
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...
1
7576
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
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
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.

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.