python: equivalent to Javascript "||" to override non-truthful value
Posted
by Jason S
on Stack Overflow
See other posts from Stack Overflow
or by Jason S
Published on 2010-05-18T15:33:16Z
Indexed on
2010/05/18
15:40 UTC
Read the original article
Hit count: 199
In Javascript I can do this:
function A(x) { return x || 3; }
This returns 3 if x is a "non-truthful" value like 0, null, false, and it returns x otherwise. This is useful for empty arguments, e.g. I can do A()
and it will evaluate as 3.
Does Python have an equivalent? I guess I could make one out of the ternary operator a if b else c
but was wondering what people use for this.
© Stack Overflow or respective owner