0

I'm trying to learn about the Lambda function in Python by messing up the code to observe what errors Python would throw in various scenarios.
Can anyone please explain what exactly is the meaning of following message displayed by the IDLE and how to interpret it?

def myfunc(n):
    return lambda a:a*n

myfunc(2)   

error:

(function myfunc.. at 0x037800B8)

1
  • Your return statement should be indented. Commented Jan 20, 2020 at 17:25

6 Answers 6

3

Your defined function returns a lambda function. This isn't an error message, it is printing the lambda function object.

Sign up to request clarification or add additional context in comments.

1 Comment

And to expand on this, the return value here is a [lambda] function that itself will take a number and multiply it by 2. Similarly, doing tripler = myfunc(3) will return (into the tripler variable) a [lambda] function that takes a number and triples it. So you can then do print(tripler(10)) and it will print 30.
1

I'll assume you mis-typed your indentation, and that the code snippet was

def myfunc(n):
    return lambda a:a*n

myfunc(2)

the lambda operator here returns a function doing x-> n*x. So myfunc is essentially a function that returns another function.

Your code is legit, and the output of python is only the description of the object myfunc(2) returned. Let's break it down:

  • the leading function confirms that you get a function
  • The myfunc.<locals>.lambda is the name of the object. That part explains that this function was created in myfunc, using some of the parameters of myfunc (note that you use n in the lambda definition`. This is called a closure.
  • the 0x037800B8 is the memory adress this python object is stored at

Comments

1

You should interpret this as a function object or a functor. myfunc.<locals>.<lambda> its just says that it is a local variable of myfunc and its a lambda function.

myfunc(2) # is a function object
print(myfunc(2)(3)) # prints 6 which is 3 * 2

This function stores the inner variable n = 2 and then returns the called value. This concept is called functional closure.

You can get into more detail if you disassembled and dive deep if you want.

from dis import dis
def myfunc(n):
    return lambda a:a*n
dis(myfunc)

OUTPUT

➜  codebase git:(master) ✗ python temp.py
  3           0 LOAD_CLOSURE             0 (n)
              2 BUILD_TUPLE              1
              4 LOAD_CONST               1 (<code object <lambda> at 0x7fc5f671f5d0, file "temp.py", line 3>)
              6 LOAD_CONST               2 ('myfunc.<locals>.<lambda>')
              8 MAKE_FUNCTION            8
             10 RETURN_VALUE

Disassembly of <code object <lambda> at 0x7fc5f671f5d0, file "temp.py", line 3>:
  3           0 LOAD_FAST                0 (a)
              2 LOAD_DEREF               0 (n)
              4 BINARY_MULTIPLY
              6 RETURN_VALUE

Comments

0

This means that your command

myfunc(2)

returned a function (namely, a lambda function in myfunc scope). 0x037800B8 is its address in memory.

Comments

0

Looks like you're returning the entire lambda function rather than returning the result. To return the lambda function's value, surround it with parenthesis like so:

>>> def myfunc(n):
    return (lambda a: a*n)(n)

>>> myfunc(2)
4

Comments

0

You are simply returning a reference to a function. Lambdas were born to make short functions, short to type, avoiding the boilerplate of writing "def function(): .."

Try looking at the following tutorial to get the basics:

https://www.w3schools.com/python/python_lambda.asp

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.