4 ms·
I have seen it a lot of code over the years, sometimes referred to as "shorthand ternary" or the "and-or-trick". In Python and/or do not return a boolean, but o
by bionoid 5y ago
I have seen it a lot of code over the years, sometimes referred to as "shorthand ternary" or the "and-or-trick". In Python and/or do not return a boolean, but one of its input arguments. So I am going to say it is official syntax, but I don't know to what extent it's encouraged
- wruza 5y agoNot only in python, but in most scripting languages. It’s hard to find one that doesn’t. But it’s neither official, nor ternary syntax anywhere. true and false or true true ? false : true These are not equivalent. If the second argument is evaluated as false, the “ternary” breaks. Lua suggests to use this as ternary, but it has only two false values (nil, false), which reduces the number of problematic cases a little.