The Python Oracle

Python shorthand conditional

This video explains
Python shorthand conditional

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Ancient Construction

--

Chapters
00:00 Question
00:32 Accepted answer (Score 33)
00:44 Answer 2 (Score 1)
01:27 Thank you

--

Full question
https://stackoverflow.com/questions/6608...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #ifstatement #conditional

#avk47



ACCEPTED ANSWER

Score 33


Call bool on the object:

bool(bar1)



ANSWER 2

Score 1


Similar to JavaScript, you can use logical negation in Python. This uses the keyword not. One disadvantage of bool is you can change its value, as this is not a reserved word. If bool variable's value is not callable. For example, bool = 7, an error will be raised. If bool variable's value is callable. For example, bool = int, you may get an unexpected value.

JavaScript

!!bar1

Python

not not bar1