Proposed new conditional operator: "or else"

Michael Torrie torriem at gmail.com
Tue Dec 2 20:51:35 EST 2014


On 12/02/2014 10:18 AM, Roy Smith wrote:
> In the process of refactoring some code, I serendipitously created what I think is an essential new bit of Python syntax.  The “or else” statement.  I ended up with:
> 
>         sites_string = args.sites or else self.config['sites']

But isn't that syntactically equivalent of this?

sites_string = args.sites or self.config['sites']

Seems to be what you're looking for with "or else" unless I
misunderstand what you're proposing.

Doing a bit of testing in the interpreter and I find that a combination
of Python's truthiness semantics and short-circuit evaluation seems to
give a consistent result. Consider:

'a word' or False => 'a word'
'a word' or True => 'a word'
False or 'a word' => 'a word'
True or 'a word' => True

Is this similar to what you'd expect with "or else?"





More information about the Python-list mailing list