I was wondering why
True and []
returns [] instead of False
Is the expression a syntactic sugar ?
I was wondering why
True and []
returns [] instead of False
Is the expression a syntactic sugar ?
This syntactic sugar is sometimes used as a ternary operator in Python
C++: someVar = someCondition ? valueIfTrue : valueIfFalse;
Python: someVar = someCondition and valueIfTrue or valueIfFalse
Edit: It turns out, as per comments :), this is a major pitfall in Python and should be replaced by
someVar = valueIfTrue if condition else valueIfFalse
The answer is found at 5.10. Boolean Expressions: