Python:
x if condition else y
Example:
val = val() if callable(val) else val
greeting = ("Hi " + name) if name != "" else "Howdy pardner"
This is often called "the ternary operator" because it has three operands. However, the term "ternary operator" applies to any operation with three operands. It just happens that most programming languages don't have any other ternary operators, so saying "the" is unambiguous. However, I'd call it the if/else
operator or a conditional expression.
In Python, due to the way the and
and or
operators work, you can also use them in some cases for things you'd usually use the ternary operator for in C-derived languages:
# provide a default value if user doesn't enter one
name = raw_input("What is your name? ") or "Jude"
print "Hey", name, "don't make it bad."
# call x only if x is callable
callable(x) and x()