Usage of 'and' in Python -


this question has answer here:

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

Popular posts from this blog

c# - Validate object ID from GET to POST -

php - Find a regex to take part of Email -

javascript - Function overwritting -