Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Return type of operator on inherited class

Reply
Thread Tools

Return type of operator on inherited class

 
 
looping
Guest
Posts: n/a
 
      03-05-2007
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 ?

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-05-2007
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
 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      03-05-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::operator<<(basic_ostream&, type) vs. std::basic_ostream::operator(type)... ? Martin T. C++ 7 03-10-2008 10:42 AM
'Class.inherited' v. 'inherited' syntax inside Class 7stud -- Ruby 11 11-09-2007 06:45 PM
Accessing inherited operator<< in base class from derived class Alicia C++ 3 11-24-2004 03:35 AM
Return by value -- primitive type vs class type DaKoadMunky C++ 8 05-14-2004 09:46 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57