0

How can I translate the following lambda expression into normal Python functions? What mistakes did I make, and what are the best ways to approach them?

love = lambda x: lambda y: lambda z: x + y * z

def love(x):
    def love(y):
        def love(z):
            return x + y * z
        return love
print(love(x))
print(love(y))
print(love(z))
Ken Kinder
  • 12,654
  • 6
  • 50
  • 70
  • Relates to [Understanding nested lambda function behaviour in python](https://stackoverflow.com/questions/36391807/understanding-nested-lambda-function-behaviour-in-python) – DarrylG Jun 14 '20 at 13:41

2 Answers2

2

It should return function for first two arguments.

def love(x):
    def f1(y):
        def f2(z):
            return x+y*z
        return f2
    return f1

print(love(2)(3)(4))  # return 2+3*4
# 14
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
1
lamda x, y, z : x + y * z

therefore:

def func(x, y, z):
    return x + y * z
jupiterbjy
  • 2,882
  • 1
  • 10
  • 28
  • 1
    I would have thought so too! – George Udosen Jun 14 '20 at 13:33
  • Post is a nested lambda expression [Understanding nested lambda function behaviour in python](https://stackoverflow.com/questions/36391807/understanding-nested-lambda-function-behaviour-in-python) so returns a function not a number. – DarrylG Jun 14 '20 at 13:45
  • Well, I thought he didn't know about lambda taking multiple inputs. – jupiterbjy Jun 14 '20 at 13:52