You can use the or
operator to assign a “default value” to a variable. This works because a or b
is equivalent to a if a else b
. Thus you could write
lista = GETLISTA() or some_default
Notice, however, that this will also use some_default
if GETLISTA()
evaluates to an empty list []
, or any other “falsy” value, not only if it returns None
. This may or may not be a problem in your case. Alternatively, use the ternary operator
lista = GETLISTA()
lista = some_default if lista is None else lista
or a simple if
check (slightly longer, but IMHO clearer than the ternary):
lista = GETLISTA()
if lista is None:
lista = some_default
1
solved Python get instruction none [closed]