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
  • 414,977
  • 106
  • 935
  • 1,083
user1187968
  • 4,829
  • 10
  • 49
  • 112

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
  • 414,977
  • 106
  • 935
  • 1,083