What is the most pythonic way to convert to boolean based on the truthiness of the object?
return bool(an_object)
or
if an_object:
return True
else:
return False
or something else entirely?
In this instance we can't get by relying on the truthiness of the object.
If you really need a boolean, use bool(something)
...
However, usually you don't need a boolean so rather than return bool(something)
, you can just return something
and the user can then decide what context to use it in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With