isedwards
isedwards

Reputation: 2509

Using python's in-place addition with unpacked tuples

Is there a reason why python can not perform an in-place add operation when unpacking tuples, and is there a simple way around this?

E.g.

>>> x, y = (5, 0)
>>> x, y += (1, 8)
SyntaxError: illegal expression for augmented assignment

The alternatives are ugly and not very obvious for code maintenance:

>>> x, y = (5, 0)
>>> x, y = map(sum, zip((x, y), (1, 8)))

Upvotes: 2

Views: 861

Answers (4)

jonrsharpe
jonrsharpe

Reputation: 122052

If you will always have two parts, you could use complex literals to represent the pair of values (so x would be the real part, and y the imaginary part):

>>> x_y = 5 + 0j
>>> x_y += 1 + 8j
>>> x_y.real, x_y.imag
(6.0, 8.0)

Obviously this looks a little complex (!) when you're only doing one operation, but if you're using lots of paired values it will work well. However, it is considered a hack and makes your code less readable (people may wonder what's doing on with .real and .imag).


A better alternative is to build your own numeric type to hold the related values. For example:

>>> from collections import namedtuple
>>> class XY(namedtuple('XY', 'x y')):
    def __repr__(self):
        return 'XY({0.x!r}, {0.y!r})'.format(self)
    def __add__(self, other):
        return XY(self.x + other.x, self.y + other.y)


>>> xy = XY(5, 0)
>>> xy += XY(1, 8)
>>> xy
XY(6, 8)

This is more readable and more flexible for adaptation to larger groups of numbers, whereas complex numbers can only hold two values. With minor tweaks, XY.__add__ could also accept any iterable of length two, so that e.g. xy += (1, 8) would also work.


In terms of why your original attempt doesn't work, note that the augmented assignment documentation states that (emphasis mine):

An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once.

Upvotes: 5

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

You could use operator.add with * which might make your intentions more obvious that you are adding each subelement:

from operator import add
x, y = map(add, *((x, y), (1, 8)))

It is also documented grammar-token-augmented_assignment_stmt:

augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression)

augtarget ::= identifier | attributeref | subscription | slicing

augop ::= "+=" | "-=" | "*=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|="

An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once.

There is also an old discussion on mail.python.org discussing the issue.

Upvotes: 4

Vlad
Vlad

Reputation: 18633

Not sure it fits with how Python tuples work normally:

>>> (1,2)+(3,4)
(1, 2, 3, 4)

rather than (4,6).

For some syntactic convenience, you can also do something like:

class pair(object):
    def __init__(self, *a):
        self.a = a
    def __add__(self, t):
        return map(sum, zip(self.a, t))

x, y = (5, 0)
x, y = pair(x, y) + (8, 1)
print x,y

Upvotes: 3

Simeon Visser
Simeon Visser

Reputation: 122376

Tuples are immutable. You can only create a new tuple with the desired values.

Upvotes: 2

Related Questions