0

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
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
user1187968
  • 5,950
  • 15
  • 62
  • 136

1 Answers1

5

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
Community
  • 1
  • 1
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148