Questions tagged [augmented-assignment]

Augmented assignment (or compound assignment) is the name given to certain assignment operators in certain programming languages (especially those derived from C). An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable. A simple example is x += 1 which is expanded to x = x + 1.

Augmented assignement Wiki page

41 questions
171
votes
9 answers

Why does += behave unexpectedly on lists?

The += operator in python seems to be operating unexpectedly on lists. Can anyone tell me what is going on here? class foo: bar = [] def __init__(self,x): self.bar += [x] class foo2: bar = [] def __init__(self,x): …
eucalculia
  • 1,727
  • 2
  • 11
  • 6
12
votes
7 answers

Adding a string to a list using augmented assignment

>>> b = [] >>> c = '1234' >>> b += c >>> b ['1', '2', '3', '4'] >>> What is happening here? This should not work, right? Or am I missing something obvious? >>> b = [] >>> c = '1234' >>> b + c Traceback (most recent call last): File…
joaquin
  • 82,968
  • 29
  • 138
  • 152
9
votes
1 answer

Can I use += on multiple variables on one line?

While shortening my code I was cutting down a few variable declarations onto one line- ##For example- going from- Var1 =15 Var2 = 26 Var3 = 922 ##To- Var1, Var2, Var3 = 15, 26, 922 However, when I tried doing the same thing to this…
8
votes
1 answer

Is there an equivalent of `sum()` builtin which uses augmented assignment?

Is there any standard library/numpy equivalent of the following function: def augmented_assignment_sum(iterable, start=0): for n in iterable: start += n return start ? While sum(ITERABLE) is very elegant, it uses + operator instead…
abukaj
  • 2,582
  • 1
  • 22
  • 45
7
votes
2 answers

Python operator precedence with augmented assignment

It seems this question only answered for Java but I would like to know how it works in Python. So are these the same? a += b / 2 and a += (b / 2)
Manngo
  • 829
  • 7
  • 24
7
votes
2 answers

python augmented assignment for boolean operators

Does Python have augmented assignment statements corresponding to its boolean operators? For example I can write this: x = x + 1 or this: x += 1 Is there something I can write in place of this: x = x and y To avoid writing "x" twice? Note that…
nonagon
  • 3,271
  • 1
  • 29
  • 42
6
votes
3 answers

Why does augmented assignment behave differently when adding a string to a list

I am in a very interesting situation and I am so surprised. actually I thought both i += 1 and i = i + 1 are same. but it'is not same in here; a = [1,2] a += "ali" and output is [1,2,"a","l","i"] but if I write like that; a = [1,2] a = a +…
Njx
  • 121
  • 6
4
votes
1 answer

pytorch's augmented assignment and requires_grad

Why does: with torch.no_grad(): w = w - lr*w.grad print(w) results in: tensor(0.9871) and with torch.no_grad(): w -= lr*w.grad print(w) results in: tensor(0.9871, requires_grad=True) Aren't both operations the same? Here is…
Tony Power
  • 1,033
  • 11
  • 23
4
votes
1 answer

How can I make a read-only property mutable?

I have two classes, one with an "in-place operator" override (say +=) and another that exposes an instance of the first through a @property. (Note: this is greatly simplified from my actual code to the minimum that reproduces the problem.) class…
4
votes
1 answer

Magic method for `self[key] += value`?

There is a Python magic method __setitem__ for assignment = to sequence types. Are there magic methods for augmented assignment += at the container level? The operation does appear to break down gracefully to an augmented assignment of the…
Bob Stein
  • 16,271
  • 10
  • 88
  • 101
4
votes
4 answers

How to implement "__iadd__()" for an immutable type?

I would like to subclass an immutable type or implement one of my own which behaves like an int does as shown in the following console session: >>> i=42 >>> id(i) 10021708 >>> i.__iadd__(1) Traceback (most recent call last): File "", line…
martineau
  • 119,623
  • 25
  • 170
  • 301
3
votes
6 answers

"+=" causing SyntaxError in Python

n = 1 p = 4 print n += p gives me: File "p7.py", line 17 print n += p SyntaxError: invalid syntax How can this problem be fixed?
jason
  • 4,721
  • 8
  • 37
  • 45
3
votes
1 answer

Python inplace Boolean operator

Python has inplace operators such as -= and |= for arithmetic and bit operations: FLAG_FOO = 1 << 0 FLAG_BAR = 1 << 1 mask = FLAG_FOO mask |= FLAG_BAR assert mask == 3 == FLAG_FOO | FLAG_BAR Is there an equivalent for actual True/False Booleans?
z0r
  • 8,185
  • 4
  • 64
  • 83
3
votes
0 answers

Pycharm unresolved attribute reference warning

Please consider the following code: import numpy as np r = [1, 0, -1, 0] bins = np.fft.fft(r) / len(r) x = bins.view(float) Given the above code PyCharm returns this warning: Unresolved attribute reference 'view' for class 'int' If I split line 4…
MaxPowers
  • 5,235
  • 2
  • 44
  • 69
3
votes
4 answers

python list comprehension vs +=

Today I was trying to find a method, to do some processing on strings in python. Some more senior programmer than I'm said not to use += but use ''.join() I could also read this in eg:…
1
2 3