[Python-ideas] Unpacking iterables for augmented assignment

Jonathan Fine jfine2358 at gmail.com
Mon Aug 27 02:25:00 EDT 2018


James has suggested that Python be enhanced so that
>>> a, b += c, d
is a short-hand for
>>> a += c
>>> b += d

Myself, James and Matthew wrote

>> > Does it IN PRACTICE bring sufficient benefits to users?

>> I found myself needing this when I was writing a monte-carlo
>> simulation in python that required incrementing a tallying counter
>> from a subroutine.

> Wouldn't a numpy array be very suited for this kind of task?

Perhaps, James, you might like to refactor your code so that
>>> tally += simulation(args)
does what you want.

As Matthew points out, you could use numpy.array. Or code your own
class, by providing __add__ and __iadd__ methods.

>>> import numpy
>>> a = numpy.array([1, 2])
>>> b = numpy.array([3, 4])
>>> a + b
array([4, 6])
>>> a += b
>>> a
array([4, 6])

-- 
Jonathan


More information about the Python-ideas mailing list