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 if condition is True, otherwise false_value.

true_value and false_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 of true_value or false_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), the ifelse 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 like filter, 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 as false_value

  • false_value – Value returned if condition is False. Must be the same type as true_value

Returns

resulttrue_value if condition is True, otherwise false_value

Return type

same as true_value and false_value

Example

>>> import descarteslabs.workflows as wf
>>> wf.ifelse(True, "yep!", "nope").inspect()  
"yep!"
>>> wf.ifelse(False, "yep!", "nope").inspect()  
"nope"