Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most pythonic way to convert to boolean?

Tags:

python

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.

like image 729
chmod 777 j Avatar asked Dec 25 '22 22:12

chmod 777 j


1 Answers

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.

like image 112
mgilson Avatar answered Feb 10 '23 19:02

mgilson