Conditionals¶
Functions:
ifelse (condition, true_value, false_value) |
An if-else statement: returns true_value if condition is True, otherwise false_value . |
-
ifelse
(condition, true_value, false_value)[source]¶ An if-else statement: returns
true_value
ifcondition
is True, otherwisefalse_value
.true_value
andfalse_value
must be the same type. (Note this is different from a Python if-else statement.)ifelse
“short-circuits” like a Python conditional: only one oftrue_value
orfalse_value
will actually get computed.Note
Since Workflows objects cannot be used in Python
if
statements (their actual values aren’t known until they’re computed), theifelse
function lets you express conditional logic in Workflows operations.However,
ifelse
should be a last resort for large blocks of logic: in most cases, you can write code that is more efficient and easier to read using functionality likefilter
,map
, empty Image/ImageCollection handling,pick_bands(allow_missing=True)
,Dict.get
, etc.Parameters: - condition (Bool) – The condition
- true_value – Value returned if
condition
is True. Must be the same type asfalse_value
- false_value – Value returned if
condition
is False. Must be the same type astrue_value
Returns: result –
true_value
ifcondition
is True, otherwisefalse_value
Return type: same as
true_value
andfalse_value
Example
>>> import descarteslabs.workflows as wf >>> wf.ifelse(True, "yep!", "nope").inspect() "yep!" >>> wf.ifelse(False, "yep!", "nope").inspect() "nope"