Returning the likelihood of a variable rather than its value?
Consider this code:
test_string = 'not empty'
if test_string:
return True
else:
return False
I know I can build a conditional for this:
return True if test_string else False
However, I don't like testing whether a boolean is true or false, when I would rather return a boolean. How can I just get my truth back?
+3
K Engle
source
to share
1 answer
You can use bool
:
return bool(test_string)
Demo:
>>> bool('abc')
True
>>> bool('')
False
>>>
+9
iCodez
source
to share