7

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
  • 3
    Short answer: no. Longer answer: the boolean operators are never going to affect the operands, so an in-place version is not going to get you anything. You either bind to x or to y, not alter the object referenced by x. Commented Oct 16, 2014 at 16:42

2 Answers 2

4

The equivalent expression is &= for and and |= for or.

>>> b = True
>>> b &= False
>>> b
False

Note bitwise AND and bitwise OR and will only work (as you expect) for bool types. bitwise AND is different than logical AND for other types, such as numeric

>>> bool(12) and bool(5)   # logical AND
True

>>> 12 & 5    # bitwise AND
4

Please see this post for a more thorough discussion of bitwise vs logical operations in this context.

Sign up to request clarification or add additional context in comments.

9 Comments

No, not really. This only works on boolean values. On everything else it'll be an error or bitwise and.
+1 even though thats not exactly the same and ... it certainly works for simple boolean types
@JoranBeasley: The answer must make that explicit, however. Right now it just looks like someone got confused between bitwise and boolean operators, just because the boolean type treats the two the same.
@JoranBeasley: besides, the OP would have to make it very explicit they are only working with booleans for this to be a viable answer.
@MartijnPieters you are correct, hopefully I made that more clear now.
|
2

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 operators on the other hand cannot be translated to an in-place operation; for x = x and y you either rebind x to x, or you rebind it to y, but x itself would not change.

As such, x and= y would actually be quite confusing; either x would be unchanged, or replaced by y.

Unless you have actual boolean objects, do not use the &= and |= augmented assignments for the bitwise operators. Only for boolean objects (so True and False) are those operators overloaded to produce the same output as the and and or operators. For other types they'll either result in a TypeError, or an entirely different operation is applied. For integers, that's a bitwise operation, sets overload it to do intersections.

6 Comments

Sorry to open an old topic, yet Martjin's explanation why no augmented versions of boolean operators exist is not exact. In fact the Python reference manual states that the in-place behavior is optional and performed only if it possible (see Augmented assignment statements).
As an example, if x = 1, the assignment x += 3 works and rebind x to 4, without modifying the number 1 in-place.
@davidedb: there is no boolean augmented operation however. There is no and= or or=, because the rebinding would be dependent on the outcome of the operator independent of the type.
@davidedb: for integers, += doesn't update the left-hand object in-place. For lists it does do this. But for a boolean operator, it would sometimes do it, sometimes not. That's something entirely different therefor.
@Martijin Pieters: As I wrote, I was commenting your statement that "Augmented assignment exist to give mutable left-hand operands the chance to alter the object in-place, rather than create a new object." and I showed an example of an augmented operator (+=), which rebound its left-end object to a different one, without altering its value in-place. I've not wrote anywhere that was a general rule and that it worked for booleans.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.