13

How can I get a lambda function's name?

Using the __name__ property returns <lambda>:

def myfunc():
    pass
myfunc_l = lambda :None

print myfunc.__name__
print myfunc_l.__name__

>>myfunc
>><lambda>

I was expecting myfunc_l but I got <lambda>

15
  • 2
    What name do you expect? Commented Apr 10, 2014 at 10:16
  • 5
    I think there is a reason why lambdas are sometimes called anonymuous functions. Commented Apr 10, 2014 at 10:17
  • 3
    @OP: so, if you assigned the same lambda to a second variable, what would you expect the name to be? Commented Apr 10, 2014 at 10:18
  • 2
    What do you expect print(lambda: None) to output? Commented Apr 10, 2014 at 10:19
  • 1
    @OP: because, sometimes when you assign a lambda to a variable, it takes the name of that variable, but sometimes it doesn't? Commented Apr 10, 2014 at 10:22

4 Answers 4

28

Lambdas are anonymous, which means they do not have a name.

You can always assign a name to __name__ if you feel they should have one anyway:

myfunc_l = lambda: None
myfunc_l.__name__ = 'foo'

Note that Python cannot know that you assigned the lambda function object to a specific name; the assignment takes place after the lambda expression was executed. Remember, you don't even have to assign a lambda:

result = (lambda x: x ** 2)(4)

or you could store the lambda in a list:

several_lambdas = [lambda y: (y // 2) for y in range(10, 20)]

and in neither context is there a name to assign to these objects.

Full-blown function definitions on the other hand are statements, not expressions, and the def statement requires that you specify a name. You can never use a def statement without naming the resulting function, which is why Python can assign the name to the object:

>>> def foo(): pass
...
>>> print foo.__name__
'foo'

You can still assign foo to something else, delete the foo global reference, rename the function object by assigning to the __name__ attribute, but it won't change the nature of the function. lambdas are the same, really, apart from the fact that there's no context to set the initial name (and the fact that they can only represent a single expression).

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

Comments

5

Work around:

Using the inspect module you can do this:

from inspect import getsource
get_lambda_name = lambda l: getsource(l).split('=')[0].strip()
print(get_lambda_name(get_lambda_name))

# result: get_lambda_name

This way you obtain the source code and extract the "anonymous" name. Note: this doesn't work in Idle command line.

Comments

2

The Lambda function name is always available as in environment variable, so should be accessible as:

os.environ['AWS_LAMBDA_FUNCTION_NAME']

https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime

2 Comments

This has nothing to do with the question asked.
came here from google, the answer fits very well to the question title and that was what I was actually looking for
-1

You can get it from the lambda handler context argument.

See here: https://docs.aws.amazon.com/lambda/latest/dg/python-context.html

print("function_name:", context.function_name)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.