简体   繁体   中英

Usage of arithmetic operations on bool values True and False

In python, there is such a feature - True and False can be added, subtracted, etc

Are there any examples where this can be useful?

Is there any real benefit from this feature, for example, when:

  • it increases productivity
  • it makes the code more concise (without losing speed) etc

While in most cases it would just be confusing and completely unwarranted to (ab)use this functionality, I'd argue that there are a few cases that are exceptions.

One example would be counting. True casts to 1 , so you can count the number of elements that pass some criteria in this fashion, while remaining concise and readable. An example of this would be:

valid_elements = sum(is_valid(element) for element in iterable)

As mentioned in the comments, this could be accomplished via:

valid_elements = list(map(is_valid, iterable)).count(True)

but to use .count(...) , the object must be a list, which imposes a linear space complexity (iterable may have been a constant space generator for all we know).

Another case where this functionality might be usable is as a play on the ternary operator for sequences, where you either want the sequence or an empty sequence depending on the value. Say you want to return the resulting list if a condition holds, otherwise an empty list:

return result_list * return_empty

or if you are doing a conditional string concatentation

result = str1 + str2 * do_concatenate

of course, both of these could be solved by using python's ternary operator:

return [] if return_empty else result_list
...
result = str1 + str2 if do_concatenate else str1

The point being, this behavior does provide other options in a few scenarios that isn't all too unreasonable. Its just a matter of using your best judgement as to whether it'll cause confusion for future readers (yourself included).

I would avoid it at all cost. It is confusing and goes against typing. Python being permissive does not mean you should do it ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM