473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Return type of operator on inherited class

Hi,
my question is on this example:

class MyStr(str):
def hello(self):
print 'Hello !'

s1 = MyStr('My string')
s2 = MyStr('My second string')

s1.hello()
s2.hello()

s = s1 + s2

s.hello()
>>>
Hello !
Hello !
Traceback (most recent call last):
File "<string>", line 204, in run_nodebug
File "<module1>", line 13, in <module>
AttributeError: 'str' object has no attribute 'hello'

How could I get a type MyStr from the 'plus' (__add__) operation
without overriding it in my class ?
I need to override *every* operator I like to use ?

Mar 5 '07 #1
2 1026
looping wrote:
Hi,
my question is on this example:

class MyStr(str):
def hello(self):
print 'Hello !'

s1 = MyStr('My string')
s2 = MyStr('My second string')

s1.hello()
s2.hello()

s = s1 + s2

s.hello()
>>>>
Hello !
Hello !
Traceback (most recent call last):
File "<string>", line 204, in run_nodebug
File "<module1>", line 13, in <module>
AttributeError: 'str' object has no attribute 'hello'

How could I get a type MyStr from the 'plus' (__add__) operation
without overriding it in my class ?
You can't.
I need to override *every* operator I like to use ?
Yes.

Diez
Mar 5 '07 #2
looping wrote:
Hi,
my question is on this example:

class MyStr(str):
def hello(self):
print 'Hello !'

s1 = MyStr('My string')
s2 = MyStr('My second string')

s1.hello()
s2.hello()

s = s1 + s2

s.hello()

Hello !
Hello !
Traceback (most recent call last):
File "<string>", line 204, in run_nodebug
File "<module1>", line 13, in <module>
AttributeError: 'str' object has no attribute 'hello'

How could I get a type MyStr from the 'plus' (__add__) operation
without overriding it in my class ?
I need to override *every* operator I like to use ?
You will not need to override if you

s = MyStr(s1 + s2)

James
Mar 5 '07 #3

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

Similar topics

6
by: Juha Tenhosaari | last post by:
Hello, I wonder if anyone could tell me why the operator= is not inherited into the derived class in the following code: class A { public: void operator=(int i){a=i;} int a;
5
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class...
7
by: Santi | last post by:
I have two classes: Product and Fruit which inherits from Product. If I try to narrow the reference to the base type by a cast, I always get a reference to the inherited type. For example: ...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
5
by: tom | last post by:
Hi, I'm overriding my operator new and operator delete for two classes, one inherited from the other, so I can use my own memory pool. A simplified version of what I have is below: class...
3
by: Magnus | last post by:
i have a class which inherits from a base class class base1 { } class child1 : base1 { } how can I determine if child1 is of base1? gettype returns type of child1, but I need to know the...
7
by: Daniel Kraft | last post by:
Hi all! I'm getting some for me "unexpected behaviour" from g++ 4.1.2 when compiling this simple test program: #include <cstdlib> #include <cstdio> using namespace std; template<typename...
12
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.