Monday, March 10, 2008

 

from PHP to Python

I recommend to read 'diveintopython', freely available at www.diveintopython.org

Great news: in python, 'x and y or z' does not return True or False, but the last element that permited to evaluate (or not) the expression.
ex: "0 and 'b' or 'c'" returns 'c'
With this particularity, we have an equivalent of "bool ? a : b" that exists in PHP but not in Python ! (provided 'a' does not evaluate to False when 'bool' is true...)
simply write: bool and a or b
if 'a' can evaluates to false, a quick workaround is to write it this way:
"(bool and [a] or [b])[0]"
(taken from diveintopython, section 4.6)

Comments:
Good words.
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?