How can I convert the following to a lambda function in Python (v 2.7) ?
def my_func(obj):
if obj.type:
return obj.name
else:
return obj.type
How can I convert the following to a lambda function in Python (v 2.7) ?
def my_func(obj):
if obj.type:
return obj.name
else:
return obj.type
You would need to use a ternary operator / conditional expression:
lambda obj: obj.name if obj.type else obj.type
Though, it seems you need to flip the things you return - return type
only if is truthy:
lambda obj: obj.type if obj.type else obj.name