3 ms·
Obviously a minor point, but if x: return True else: return False is weirdo-code. Just use return x, maybe cast to bool if you need to.
by hawkice 10y ago
Obviously a minor point, but if x: return True else: return False is weirdo-code. Just use return x, maybe cast to bool if you need to.
- cool_guy_this 10y agoreturn x is way more pythonic, because it's much clearer and any non-boolean value can be used in boolean contexts as well.
- coldtea 10y agoYou can also "return not not x" which turns the value into its boolean true/false equivalent ("!!x" in a C-like language). Or "return bool(x)" to make this more explicit. Those would be handy e.g. if the code that uses your result behaves differently 0 and False.