Behavior of += (was Re: [Python-Dev] Customization docs)

Donn Cave donn at u.washington.edu
Tue Jun 4 20:06:22 EDT 2002


Quoth bokr at oz.net (Bengt Richter):
...
| Here is a contrived case where += lets you use a
| mutable-lhs-target-expression-with-side-effect and
| get only one side effect. There are obviously other
| ways, but at least += is convenient (and presumably
| faster in a case like this).
|
|  >>> a=[0]
|  >>> def foo():
|  ...     print 'side effect'
|  ...     return a
|  ...
|  >>> foo()
|  side effect
|  [0]
|  >>> foo()[0]+=1
|  side effect
|  >>> a
|  [1]
|  >>> foo()[0] = foo()[0]+1
|  side effect
|  side effect
|  >>> a
|  [2]

I imagine you have some actual application for something like this,
but the example doesn't seem very compelling - like you say, there
are obviously other ways.  If they're less convenient, maybe they're
easier on the eyes.

But it's probably a moot point.  Whether the current behavior
of += is a wart or not, it's not likely to be removed.

	Donn Cave, donn at u.washingon.edu



More information about the Python-list mailing list