3

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
fountainhead
3,7421 gold badge11 silver badges18 bronze badges
asked Mar 9, 2019 at 8:30
6
  • 2
    This seems like a broad question, but yeah, It's pretty useful at times. Commented Mar 9, 2019 at 8:34
  • 4
    valid_elements = sum(is_valid(element) for element in iterable) Commented Mar 9, 2019 at 8:35
  • 1
    You could, but that means you'd have to make it into a list to use .count(). This of course imposes a linear space complexity on something that may have even been constant (if it were a generator or something), but is doable. Commented Mar 9, 2019 at 9:09
  • 1
    Another example would be list or string concatenation. str1 + str2 * include_str2 Commented Mar 9, 2019 at 9:12
  • 2
    It's worth noting that bool is actually a subclass of int Commented Mar 10, 2019 at 12:34

2 Answers 2

2

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).

answered Mar 9, 2019 at 9:33
Sign up to request clarification or add additional context in comments.

4 Comments

Could you pls elaborate on your point about imposing a linear space complexity?
@fountainhead in order to use .count(), the iterable must be made into a list, which means that it must be stored entirely in memory at once. However, what if iterable was range(1000000). Range only takes constant space, generating the next element as its requested. By transforming this into a list, suddenly we need O(n) space to store it.
In this thread, we're discussing something that has or generates bool values, so, range(1000000) is perhaps a bad example to illustrate your point. But yes, it could be a generator function that generates 1000000 bool values, of which we are interested in counting the Trues. I don't see any documentation that says sum won't do dumb things like assembling all the elements and then summing them up. But yeah, I guess it's a safe assumption to make.
@fountainhead my bad, I should have explicitly stated that I mean if the iterable from the first example was range(...) (where elements are then passed to is_valid(...)).
0

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

answered Mar 9, 2019 at 8:35

2 Comments

There are exceptions to every rule. blanket statements like these should be said very carefully. I do not agree with this statement when it comes to bools in python, as is, i have definitely used them in niche cases before without losing out on readibility.
@CedricDruck: I'm not sue about "going against typing", given the rather interesting but little known fact that bool is actually a subclass of int

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.