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
0
votes
2 answers

UnboundLocalError when using += on list. Why is `nonlocal` needed here when directly calling __iadd__ works fine?

Consider this code: def main(): l = [] def func(): l += [1] func() print(l) if __name__ == '__main__': main() It will produce: Traceback (most recent call last): File…
tahsmith
  • 1,643
  • 1
  • 17
  • 23
0
votes
0 answers

Function parameter is reference?

def add_list(p): p = p + [1] p1 = [1, 2, 3] add_list(p1) print p1 res:[1, 2, 3] BUT def add_list(p): p += [1] p1 = [1, 2, 3] add_list(p1) print p1 res:[1, 2, 3, 1] I don't know why, can someone explain this? What's the main difference…
kker neo
  • 133
  • 4
0
votes
2 answers

Python ternary conditional for joining string

Answered by Martijn Pieters. Thank you. It is because statement vs expression. And because .join() does not mutate (is a pure function), so it needs to be assigned to a variable. Question: What is the reason for this oddity? Goal: if base == 'T': …
Luigi
  • 45
  • 1
  • 8
0
votes
2 answers

Augmented assignment in the middle of expression causes SyntaxError

In my code I have these lines: if numVotes == 0: avId in self.disconnectedAvIds or self.resultsStr += curStr When I run the code I get this error? SyntaxError: illegal expression for augmented assignment How do I fix this error?
Zach Gates
  • 4,045
  • 1
  • 27
  • 51
0
votes
0 answers

How does Fortran handle augmented assignment of arrays?

I've been working on some code lately that requires me to shift elements in an array left, right, up, and down (depending on an index i). My first thought was to try something like this: subroutine shift(f) real, intent(inout) :: f(0:8, rDim,…
0
votes
1 answer

android augmented reality image targets

I am planning to develop an augmented reality application for Android phone. Does anyone know if there is any existing framework(in java programming langueage) for augmented reality which could be used for such applications? Thanks.
user3345767
  • 349
  • 1
  • 2
  • 11
0
votes
0 answers

Why is it that Lua doesn't have augmented assignment?

Please note that this is a genuine question. I'm not rep-whoring. But if anyone feels like closing it like this one please don't close before I get an answer. That said, I'd seriously like to know why Lua doesn't have augmented assignment like most…
Plakhoy
  • 1,846
  • 1
  • 18
  • 30
-1
votes
2 answers

Is this code thread-safe in python 2.7?

Should I use atomic counter with Locking or can I use this? def somefunc(someparam): if someparam: dic['key'] +=1
oshaiken
  • 2,593
  • 1
  • 15
  • 25
-2
votes
1 answer

Python operator precedence with augmented assignment including sequence

Following up my previous question I have the following one. Are these the same in Python? a += b[1] / 2 and a += (b[1] / 2) Providing that: a has already been defined earlier as float b is a list of tuples b[1] is a tuple with a single…
-2
votes
3 answers

Why does the original list change?

Running this: a = [[1], [2]] for i in a: i *= 2 print(a) Gives [[1, 1], [2, 2]] I would expect to get the original list, as happens here: a = [1, 2] for i in a: i *= 2 print(a) Which gives: [1, 2] Why is the list in the first example…
blueFast
  • 41,341
  • 63
  • 198
  • 344
-3
votes
1 answer

python augmented assignment statements, display the results as per the output

Write a program which asks the user to enter an integer and then by using augmented assignment statements, display the results as per the output Please enter the first integer: 5 Please enter the second integer: 4 5 + 4 is 9 5 - 4 is 1 5 * 4 is 20 5…
goldie
  • 1
  • 1
1 2
3