473,372 Members | 847 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,372 software developers and data experts.

__iadd__ useless in sub-classed int

For whatever reason, I need an inproved integer. Sounds easy, let's
just subclass int:
>>class test(int):
pass

Now let's test it:
>>zed=test(0)
zed.__class__
<class '__main__.test'>
>>zed
0

So far, so good. Now let's try incrementing:
>>zed+=1
zed
1
>>zed.__class__
<type 'int'>

WTF??!
Is this a bug or is it the inevitable result of optimizing for the
case where all integers are indistinguishable?
Dec 6 '07 #1
7 1474
samwyse schrieb:
For whatever reason, I need an inproved integer. Sounds easy, let's
just subclass int:
>>>class test(int):
pass

Now let's test it:
>>>zed=test(0)
zed.__class__
<class '__main__.test'>
>>>zed
0

So far, so good. Now let's try incrementing:
>>>zed+=1
zed
1
>>>zed.__class__
<type 'int'>

WTF??!
Is this a bug or is it the inevitable result of optimizing for the
case where all integers are indistinguishable?
There has been a lengthe thread over the semantics of __iadd__ a few
weeks ago. It _can_ modify the object in question in-place (something
not possible for ints anyway), but it will ALWAYS return a reference
which will be set to the left-hand-side.

zed = zed.__iadd__(1)

So - you need to overload the __iadd__-method to return a test-instance.

Diez
Dec 6 '07 #2
On Dec 6, 1:12 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
samwyse schrieb:
For whatever reason, I need an inproved integer. Sounds easy, let's
just subclass int:
>>class test(int):
pass
Now let's test it:
>>zed=test(0)
zed.__class__
<class '__main__.test'>
>>zed
0
So far, so good. Now let's try incrementing:
>>zed+=1
zed
1
>>zed.__class__
<type 'int'>
WTF??!
Is this a bug or is it the inevitable result of optimizing for the
case where all integers are indistinguishable?

There has been a lengthe thread over the semantics of __iadd__ a few
weeks ago. It _can_ modify the object in question in-place (something
not possible for ints anyway), but it will ALWAYS return a reference
which will be set to the left-hand-side.
Thanks! I'd missed that thread, googling found it but it didn't look
noteworthy at first glance. I've not yet read the entire thread, but
I did see a reference to PEP 203.

) So, given the expression:
)
) x += y
)
) The object `x' is loaded, then `y' is added to it, and the
) resulting object is stored back in the original place.

That agrees with what I'm seeing, all right. The problem is, the
resulting object has a different type, which seems to violate the
spirit of a later paragraph:

) Writing the above expression as
)
) <x<operator>= <y>
)
) is both more readable and less error prone, because it is
) instantly obvious to the reader that it is <xthat is being
) changed, and not <xthat is being replaced by something almost,
) but not quite, entirely unlike <x>.

And that's my complaint. The value in <zedis being replaced by
something almost, but not quite, identical to the original value.
Python's internal implementation of __iadd__ for <intisn't returning
<self>, it's returning a new value belonging to the super-class. My
whole point is overloading <intwas that I'd hoped to avoid having to
write a bunch of methods to perform in-place modifications. Looks
like I stuck, however.
Dec 6 '07 #3
On Dec 6, 2007 3:02 PM, samwyse <sa*****@gmail.comwrote:
>
On Dec 6, 1:12 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
samwyse schrieb:
For whatever reason, I need an inproved integer. Sounds easy, let's
just subclass int:
>>>class test(int):
pass
Now let's test it:
>>>zed=test(0)
>>>zed.__class__
<class '__main__.test'>
>>>zed
0
So far, so good. Now let's try incrementing:
>>>zed+=1
>>>zed
1
>>>zed.__class__
<type 'int'>
WTF??!
Is this a bug or is it the inevitable result of optimizing for the
case where all integers are indistinguishable?
There has been a lengthe thread over the semantics of __iadd__ a few
weeks ago. It _can_ modify the object in question in-place (something
not possible for ints anyway), but it will ALWAYS return a reference
which will be set to the left-hand-side.

Thanks! I'd missed that thread, googling found it but it didn't look
noteworthy at first glance. I've not yet read the entire thread, but
I did see a reference to PEP 203.

) So, given the expression:
)
) x += y
)
) The object `x' is loaded, then `y' is added to it, and the
) resulting object is stored back in the original place.

That agrees with what I'm seeing, all right. The problem is, the
resulting object has a different type, which seems to violate the
spirit of a later paragraph:
The phrasing is a little awkward. Instead of "store" say "bound to the
same name as"
) Writing the above expression as
)
) <x<operator>= <y>
)
) is both more readable and less error prone, because it is
) instantly obvious to the reader that it is <xthat is being
) changed, and not <xthat is being replaced by something almost,
) but not quite, entirely unlike <x>.

And that's my complaint. The value in <zedis being replaced by
something almost, but not quite, identical to the original value.
Python's internal implementation of __iadd__ for <intisn't returning
<self>, it's returning a new value belonging to the super-class. My
whole point is overloading <intwas that I'd hoped to avoid having to
write a bunch of methods to perform in-place modifications. Looks
like I stuck, however.
Remember that in Python, name binding (which is to say, pure
assignment) is never a mutating operation. It's purely internal to the
namespace it occurs in. So even when augmented assignment returns
self, it still results in a normal Python assignment.

You're going to be struggling against the grain trying to implement a
mutable int anyway, you won't be able to make "foo = 10" mutate the
int, and even if you do create one you probably don't want it to
return true for isinstance(foo, int).
Dec 6 '07 #4
On Dec 6, 3:02 pm, samwyse <samw...@gmail.comwrote:
On Dec 6, 1:12 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
samwyse schrieb:
For whatever reason, I need an inproved integer. Sounds easy, let's
just subclass int:
>>>class test(int):
pass
Now let's test it:
>>>zed=test(0)
>>>zed.__class__
<class '__main__.test'>
>>>zed
0
So far, so good. Now let's try incrementing:
>>>zed+=1
>>>zed
1
>>>zed.__class__
<type 'int'>
WTF??!
Is this a bug or is it the inevitable result of optimizing for the
case where all integers are indistinguishable?
There has been a lengthe thread over the semantics of __iadd__ a few
weeks ago. It _can_ modify the object in question in-place (something
not possible for ints anyway), but it will ALWAYS return a reference
which will be set to the left-hand-side.

Thanks! I'd missed that thread, googling found it but it didn't look
noteworthy at first glance. I've not yet read the entire thread, but
I did see a reference to PEP 203.

) So, given the expression:
)
) x += y
)
) The object `x' is loaded, then `y' is added to it, and the
) resulting object is stored back in the original place.

That agrees with what I'm seeing, all right. The problem is, the
resulting object has a different type, which seems to violate the
spirit of a later paragraph:

) Writing the above expression as
)
) <x<operator>= <y>
)
) is both more readable and less error prone, because it is
) instantly obvious to the reader that it is <xthat is being
) changed, and not <xthat is being replaced by something almost,
) but not quite, entirely unlike <x>.

And that's my complaint. The value in <zedis being replaced by
something almost, but not quite, identical to the original value.
Python's internal implementation of __iadd__ for <intisn't returning
<self>, it's returning a new value belonging to the super-class. My
whole point is overloading <intwas that I'd hoped to avoid having to
write a bunch of methods to perform in-place modifications. Looks
like I stuck, however.
I've wondered about this myself. Seems to me, to prevent clobbering
subclasses, __iadd__ (and all of the integer and float and whatever)
methods that return new instances, should work like this (obviously I
mean in the C backend, this is just to show the behavior):

def __iadd__(self, other):
return self.__class__(self + other)

Regards,
Jordan
Dec 7 '07 #5
En Fri, 07 Dec 2007 03:01:28 -0300, MonkeeSage <Mo********@gmail.com>
escribió:
I've wondered about this myself. Seems to me, to prevent clobbering
subclasses, __iadd__ (and all of the integer and float and whatever)
methods that return new instances, should work like this (obviously I
mean in the C backend, this is just to show the behavior):

def __iadd__(self, other):
return self.__class__(self + other)
This would slow down *all* of Python, and is only useful for those who
actually inherit from some builtin class (not so common)

--
Gabriel Genellina

Dec 7 '07 #6
On Dec 7, 12:45 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 07 Dec 2007 03:01:28 -0300, MonkeeSage <MonkeeS...@gmail.com>
escribió:
I've wondered about this myself. Seems to me, to prevent clobbering
subclasses, __iadd__ (and all of the integer and float and whatever)
methods that return new instances, should work like this (obviously I
mean in the C backend, this is just to show the behavior):
def __iadd__(self, other):
return self.__class__(self + other)

This would slow down *all* of Python, and is only useful for those who
actually inherit from some builtin class (not so common)

--
Gabriel Genellina
I understand why it doesn't. It just *seems* like it should work that
way when you first run into it (and has bitten me a couple times
before). But then, I'm not Dutch. :)

Regard,
Jordan
Dec 7 '07 #7
On 2007-12-06, samwyse <sa*****@gmail.comwrote:
On Dec 6, 1:12 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
And that's my complaint. The value in <zedis being replaced by
something almost, but not quite, identical to the original value.
Python's internal implementation of __iadd__ for <intisn't returning
<self>, it's returning a new value belonging to the super-class. My
whole point is overloading <intwas that I'd hoped to avoid having to
write a bunch of methods to perform in-place modifications. Looks
like I stuck, however.
I think you don't want this.

Suppose I keep track of addition information (eg a boolean "is_even = value ==
value//2") Since the base class doesn't know about this, it may return an
incorrect instance.

Albert

Dec 10 '07 #8

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

Similar topics

1
by: pyguy2 | last post by:
Issues of socket programming can be wierd, so I'm looking for some comments. In my python books I find exclusive use of socket.close(). From my other readings, I know about a "partial close...
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
1
by: Steffen Loringer | last post by:
Hi all, are tools available to check my source code for unused stuff? Like variables once defined for test purposes or Sub's which are never called? Thanks Steffen
21
by: Paul Rubin | last post by:
I've always found the string-building idiom temp_list = for x in various_pieces_of_output(): v = go_figure_out_some_string() temp_list.append(v) final_string = ''.join(temp_list) ...
3
by: Ronny Mandal | last post by:
Can someone please explain or point me to articles regarding these two methods? Thanks.
11
by: Rickie | last post by:
Dear fellow software engineers, Is the web menu in asp 2.0 essentielle a useless control ? How many people( including msdn itself) are using server menu controls? Its a menu which always...
12
by: Ron M. Newman | last post by:
hi, without going case-by-case, how do I know a "ToString" method returns useful information, like for Sytem.Int32, or useless information like for System.Drawing.Bitmap (returning the name of...
2
by: Moon | last post by:
class Vec(list): def __init__(self): list.__init__(self, ) def __iadd__(self, other): assert isinstance(other, Vec) self += other self += other print "right now, v is: ", self, " as you'd...
9
by: Lars Eighner | last post by:
In the code below, the line "unsigned int ktop,kbot;" seems to be useless as ktop and kbot are never used. Yet, this work with it and breaks without it. Any idea why? #include <stdio.h>
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...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.