3 ms·
> Sometimes you do want to do math on bools.. Can you provide a concrete example for when this would be useful? I assume you're not talking about binary arith
by politician 8y ago
> Sometimes you do want to do math on bools..
Can you provide a concrete example for when this would be useful? I assume you're not talking about binary arithmetic.
- duckerude 8y agosum(x > 3 for x in iterable) True == 1, False == 0, so this counts how many items in iterable are greater than 3. But I think that's the only non-dirty way I've used it, and if that wasn't possible a trivial count() function could do the job. If I remember correctly, bool subclasses int mostly for compatibility with pre-existing conventions. People were already defining their own "True = 1" and "False = 0" constants before they became part of the language.
- cma 8y agoIf it wasn't possible you could still do: sum(x > 3 for int(x) in iterable) And avoid a class of errors in other places.
- duckerude 8y agoThat code is invalid, because you're using "int(x)" as an assignment target. What would you expect it to do?
- cma 8y agosorry: sum(int(x) > 3 for x in iterable)
- duckerude 8y agoThat works better if you expect things in iterable that aren't ints, but it's still summing bools. int(x) > 3 returns a bool, so in the end it's still doing something similar to sum([True, False, False, True, True, False]) But if bools didn't implicitly behaves like ints, but could still be converted to ints, you could do this: sum(int(x > 3) for x in iterable)
- eesmith 8y agoHere are some examples from the Python standard library: multiprocessing/pool.py: self._number_left = length//chunksize + bool(length % chunksize) test/test_math.py: tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1) From NumPy and SciPy: numpy-1.11.0/tools/swig/test/testArray.py:385: sys.exit(bool(result.errors + result.failures)) scipy/scipy/signal/signaltools.py:2003: n_out = n_out // down + bool(n_out % down) scipy/scipy/signal/signaltools.py:3001: n_out = x.shape[axis] // q + bool(x.shape[axis] % q) Here's one from my own code, where I allow a query to be specified through --query , or as a hex-encoded query through --hex-query or as an input file through --queries, but I only allow one of them: if bool(args.query) + bool(args.hex_query) + bool(args.queries) > 1:
- meowface 8y ago>Here's one from my own code, where I allow a query to be specified through --query , or as a hex-encoded query through --hex-query or as an input file through --queries, but I only allow one of them This would be better written as a mutually exclusive argument group. Assuming you're using argparse: https://stackoverflow.com/a/13310109 https://stackoverflow.com/a/13310109
- eesmith 8y ago"Better" only if you want the default error message. As the second rated answer for the question comments: "If you like to have more control about error message and the like, you can of course check the results later" For example, this code: import argparse parser = argparse.ArgumentParser() g = parser.add_mutually_exclusive_group() g.add_argument("--queries",help="queries help") g.add_argument("--hex-query",help="hex-query help") g.add_argument("--query",help="query help") args = parser.parse_args() print(args) When passed the command-line arguments "--hex-query AB --query CD --queries ASDF" it gives: error: argument --query: not allowed with argument --hex-query My code gives: error: Can only specify at most one of --query, --hex-query, or --queries
- politician 8y agoLooks like the libraries use the technique to handle those conditional off-by-one situations that other languages might resolve with a ternary expression. Thanks!