Is there a way to write a one line python statement for
if flag:
return True
Note that this can be semantically different from
return flag
In my case, None is expected to be returned otherwise.
I have tried with "return True if flag", which has syntactic error detected by my emacs.
return True if flag doesn't work because you need to supply an explicit else. You could use:
return True if flag else None
to replicate the behaviour of your original if statement.
You could use:
return bool(flag) or None
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