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.

nonagon
  • 3,271
  • 1
  • 29
  • 42
  • 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`. – Martijn Pieters Oct 16 '14 at 16:42

2 Answers2

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.

Community
  • 1
  • 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 4
    No, not really. This only works on boolean values. On everything else it'll be an error or *bitwise and*. – Martijn Pieters Oct 16 '14 at 16:41
  • +1 even though thats not exactly the same `and` ... it certainly works for simple boolean types – Joran Beasley Oct 16 '14 at 16:41
  • 1
    @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. – Martijn Pieters Oct 16 '14 at 16:44
  • @JoranBeasley: besides, the OP would have to make it very explicit they are only working with booleans for this to be a viable answer. – Martijn Pieters Oct 16 '14 at 16:44
  • @MartijnPieters you are correct, hopefully I made that more clear now. – Cory Kramer Oct 16 '14 at 16:44
  • And, FWIW, I've never liked that we call it "bitwise" and in python (although I call it that myself sometimes and so does the [documentation](https://docs.python.org/2/library/operator.html#operator.__and__)). Ultimately, it's whatever the type has decided the `__and__` hook method should do. i.e. for `set` types, it's intersection . . . – mgilson Oct 16 '14 at 16:45
  • @Cyber: it is more clear, but it is not a correct answer. Not unless the OP makes it explicit only booleans need to be supported. Otherwise the answer to the question is a *no*. Your answer is a nice trick, but next to useless when considering that you can use *any* type with the boolean operators. – Martijn Pieters Oct 16 '14 at 16:46
  • @mgilson: right, but in my view booleans and sets are the exception here. Granted, most types have little use for the operators otherwise.. – Martijn Pieters Oct 16 '14 at 16:47
  • Yes indeed, sorry for not being explicit. I had discovered &= but really want something that evaluates its operand as a boolean just like 'and' and 'or' do. – nonagon Oct 16 '14 at 20:17
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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 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](https://docs.python.org/2.7/reference/simple_stmts.html#augmented-assignment-statements)). – davidedb Feb 24 '16 at 14:14
  • As an example, if `x = 1`, the assignment `x += 3` works and rebind `x` to `4`, without modifying the number `1` _in-place_. – davidedb Feb 24 '16 at 14:21
  • @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*. – Martijn Pieters Feb 24 '16 at 16:29
  • @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. – Martijn Pieters Feb 24 '16 at 16:31
  • @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. – davidedb May 05 '16 at 19:56
  • @davidedb: right, but my statement doesn't preclude that. `int` objects are not mutable; the operator exists to give *mutable* objects a change to alter the object in-place. That there's no boolean augmented operator has nothing to do with any of this however. – Martijn Pieters May 05 '16 at 19:58