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
  • 462,703
  • 120
  • 1,088
  • 1,195
user1187968
  • 7,154
  • 16
  • 81
  • 152

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
  • 462,703
  • 120
  • 1,088
  • 1,195