473,411 Members | 1,998 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,411 software developers and data experts.

Short, crazy example: list-derived class, with __iadd__

class Vec(list):
def __init__(self):
list.__init__(self, [0.0, 0.0])

def __iadd__(self, other):
assert isinstance(other, Vec)
self[0] += other[0]
self[1] += other[1]
print "right now, v is: ", self, " as you'd expect"
v = Vec()
w = Vec()
w[0] = 1.0
w[1] = 2.0
print "v starts:", v

print "(w is:", w, " which is fine)"

v += w

print "(w still is:", w

print "after iadd, v: ", v, " <-- becomes None! What the hey?"
# - running it:

pypython badvec.py
v starts: [0.0, 0.0]
(w is: [1.0, 2.0] which is fine)
right now, v is: [1.0, 2.0]
(w still is: [1.0, 2.0]
later, v is: None <-- becomes None! What the hey?

pypython -V
Python 2.5.1

-- Any explanation from a guru?

Thanks much...
Aug 29 '07 #1
2 2045
__iadd__ is supposed to /return/ something, most likely self.

My bad.

On Wed, 29 Aug 2007 20:49:59 +0000, Moon wrote:
class Vec(list):
def __init__(self):
list.__init__(self, [0.0, 0.0])

def __iadd__(self, other):
assert isinstance(other, Vec)
self[0] += other[0]
self[1] += other[1]
print "right now, v is: ", self, " as you'd expect"
v = Vec()
w = Vec()
w[0] = 1.0
w[1] = 2.0
print "v starts:", v

print "(w is:", w, " which is fine)"

v += w

print "(w still is:", w

print "after iadd, v: ", v, " <-- becomes None! What the hey?"
# - running it:

pypython badvec.py
v starts: [0.0, 0.0]
(w is: [1.0, 2.0] which is fine)
right now, v is: [1.0, 2.0]
(w still is: [1.0, 2.0]
later, v is: None <-- becomes None! What the hey?

pypython -V
Python 2.5.1

-- Any explanation from a guru?

Thanks much...
Aug 29 '07 #2
Moon <no***********@zombo.comwrites:
class Vec(list):
def __init__(self):
list.__init__(self, [0.0, 0.0])

def __iadd__(self, other):
assert isinstance(other, Vec)
self[0] += other[0]
self[1] += other[1]
print "right now, v is: ", self, " as you'd expect"
return self
>

v = Vec()
w = Vec()
w[0] = 1.0
w[1] = 2.0
print "v starts:", v

print "(w is:", w, " which is fine)"

v += w

print "(w still is:", w
print "after iadd, v: ", v
>
# - running it:
v starts: [0.0, 0.0]
(w is: [1.0, 2.0] which is fine)
right now, v is: [1.0, 2.0] as you'd expect
(w still is: [1.0, 2.0]
after iadd, v: [1.0, 2.0]

-Marshall

Aug 29 '07 #3

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

Similar topics

10
by: Danny Anderson | last post by:
Hola! I have an array of shorts that represents a stream of data. This data is packed in tight, so certain elements may actually contain data for more than one variable. I am to decipher this...
12
by: Gnolen | last post by:
Hi, I am really getting crazy here! I just do not get why this happens with the borders of the td/tr! I just want a border on the bottom of the rows(or td) but I just can't do it!!! I have tried...
99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
32
by: Chumbo | last post by:
If I have this(please bear with me - this is a case of a former java guy going back to C ;-) int main () { char *tokconvert(char*); char str = "dot.delimited.str"; char *result; result =...
12
by: alistair_happencross | last post by:
In my copy of "Algorithms in C" by R. Sedgewick (3rd edition, January 1999 reprint), this is on pages 71-72: "we think of a short int as an object that can take on values between -32768 and...
13
by: momobear | last post by:
hi, is there a way to let python operate on sequence of int or short? In C, we just need declare a point, then I could get the point value, just like: short* k = buffer, //k is a point to a...
4
by: bytecolor | last post by:
Hi people, Using win32com on 2k SP3... >>> import win32com.client as w32c >>> fc = w32c.Dispatch('Featurecam.Application') >>> fc.InstallPath u'C:\\PROGRA~1\\FEATUR~1' >>> Using win32com...
0
by: Linda Jimerson | last post by:
Hello - I created a simple Bulleted Rollover List from a Listamatic resource. When I contain this list (in a DIV with a width), IE 6.0 goes crazy (makes a mess). All other browsers act...
2
by: pedalpete | last post by:
Hi All, I'm using Bill Scott's YUI carousel v5.6, and I've run into a crazy looping javascript for some reason, and I can't seem to get around it. The carousel loads a bunch of items, 7 of...
4
by: XLiIV | last post by:
I started playing with Python and love it since the very beginning, programming using Python is so ...human-like? but one thing returns to me almost everytime when I see/write the code Let's...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.