augmented-assignment

“+=” causing SyntaxError in Python

故事扮演 提交于 2020-02-22 06:23:30
问题 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? 回答1: n += p is a statement in Python, not an expression that returns a value you could print. This is different from a couple of other languages, for example Ruby, where everything is an expression. You need to do n += p print n 回答2: Assignment, including "augmented" assignment ( x op= expr as shorcut for x = x op expr ), is a statement, not an expression. So it

“+=” causing SyntaxError in Python

橙三吉。 提交于 2020-02-22 06:23:25
问题 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? 回答1: n += p is a statement in Python, not an expression that returns a value you could print. This is different from a couple of other languages, for example Ruby, where everything is an expression. You need to do n += p print n 回答2: Assignment, including "augmented" assignment ( x op= expr as shorcut for x = x op expr ), is a statement, not an expression. So it

Python operator precedence with augmented assignment

狂风中的少年 提交于 2019-12-31 00:51:35
问题 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) 回答1: Yes, those are the same. Python's augmented assignment is not an expression , it is a statement, and doesn't play in expression precedence rules. += is not an operator, and instead it's part of the augmented assignment statement syntax. So everything to the right of the += is an expression, but += itself is not, so the assignment will always be

python augmented assignment for boolean operators

China☆狼群 提交于 2019-12-06 03:24:59
问题 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 I'm aware of statements using &= , but I was looking for a statement that would work when y is any type, not just when y is a boolean. 回答1: No, there is no augmented assignment operator for the boolean operators. Augmented assignment exist to give

python augmented assignment for boolean operators

三世轮回 提交于 2019-12-04 07:19:06
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 I'm aware of statements using &= , but I was looking for a statement that would work when y is any type, not just when y is a boolean. No, there is no augmented assignment operator for the boolean operators . Augmented assignment exist to give mutable left-hand operands the chance to alter the object in-place, rather than create a new object. The boolean

How to implement “__iadd__()” for an immutable type?

我的梦境 提交于 2019-11-28 05:33:06
问题 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 "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute '__iadd__' >>> i += 1 >>> i 43 >>> id(i) 10021696 Not surprisingly, int objects have no __iadd__() method, yet applying += to one doesn't result in an error, instead it apparently creates a

Why does += behave unexpectedly on lists?

断了今生、忘了曾经 提交于 2019-11-25 22:57:29
问题 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): self.bar = self.bar + [x] f = foo(1) g = foo(2) print f.bar print g.bar f.bar += [3] print f.bar print g.bar f.bar = f.bar + [4] print f.bar print g.bar f = foo2(1) g = foo2(2) print f.bar print g.bar OUTPUT [1, 2] [1, 2] [1, 2, 3] [1, 2, 3] [1, 2, 3, 4] [1, 2, 3] [1] [2] foo += bar