Usage of 'and' in Python -
this question has answer here:
- python , / or operators return value 4 answers
i'm maintaining large codebase , found looks in python code:
... foo = bar(a, b, c) , foo return foo ...
where foo assigned boolean, , declared first time here. mean/what purpose serve/what's going on here?
python short circuit evaluation c does.
this means in "and" case, right side of and
evaluated if left side true.
so code equivalent to:
x = bar(a, b, c) if x: foo = foo else: foo = x return foo
with distinction of course, variable x not used (since not needed).
the important fact note here is, example if foo set "stringval" , bar(a,b,c) returns can interpreted true, "stringval" returned. unlike in other languages, boolean expression can have non-boolean result.
Comments
Post a Comment