[Python-bugs-list] [ python-Bugs-452252 ] __iadd__ sets object to None
noreply@sourceforge.net
noreply@sourceforge.net
Fri, 14 Sep 2001 15:19:29 -0700
Bugs item #452252, was opened at 2001-08-17 15:46
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=452252&group_id=5470
Category: Python Interpreter Core
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: John Shipman (shipman)
Assigned to: Nobody/Anonymous (nobody)
Summary: __iadd__ sets object to None
Initial Comment:
Here's my version info:
ActivePython 2.1, build 211 (ActiveState)
based on Python 2.1 (#1, Jun 15 2001, 02:52:41)
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
If I overload `+=' in a class using an __iadd__
method, the object is replaced by None. Here's
a script that demonstrates the problem:
=====================================================
#!/usr/local/bin/python
#--
# iadd: See if __iadd__ actually works
#--
class C:
def __init__ ( self, value ):
self.value = value
def __str__ ( self ):
return "C<%s>" % self.value
def __repr__ ( self ):
return str(self)
def __iadd__ ( self, x ):
self.value = self.value + x
# - - - - - m a i n - - - - -
c = C(37)
print "Before:", c
c += 10
print "Add 10:", c
=====================================================
And here's the output I get:
=====================================================
Before: C<37>
Add 10: None
=====================================================
----------------------------------------------------------------------
Comment By: Gregory Smith (gregsmith)
Date: 2001-09-14 15:19
Message:
Logged In: YES
user_id=292741
That should be 'you must return self'. __iadd__ should return self.
unlike with 'classnm::operator += (rhs)' in C++,
c += 10
is equiv to
c = c.__iadd__(10)
(if __iadd__ exists). This allows you to morph c into something
else if you want.
----------------------------------------------------------------------
Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-17 18:27
Message:
Logged In: YES
user_id=6380
You must return None.
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=452252&group_id=5470