How can I get the function object inside a lambda function
Asked Answered
C

1

2

Sorry if this is very lame, but I'm pretty new to Python.

As in Python everything is an object, I assume in every object the object itself can be get somehow. In object methods the self variable contains it. From the object reference the class object can be get (like type(self)). But how this could be got inside a lambda?

I could figure out for a normal function:

import inspect

def named_func():
    func_name = inspect.stack()[0].function
    func_obj = inspect.stack()[1].frame.f_locals[func_name]
    print(func_name, func_obj, func_obj.xxx)

named_func.xxx = 15
named_func()

The output looks like this:

named_func <function named_func at 0x7f56e368c2f0> 15

Unfortunately in a lambda the inspect.stack()[0].function gives <lambda> inside the lambda.

Is there a way to get the function object inside a lambda? Is there a way to get function object directly (not using the name of the function)? I imagined __self__, but it does not work.

UPDATE

I tried something like this in lambda:

lambda_func = lambda : inspect.stack()[0]
lambda_func.xxx = 2
print(lambda_func())

This prints:

FrameInfo(frame=<frame at 0x7f3eee8a6378, file './test_obj.py', line 74, code <lambda>>, filename='./test_obj.py', lineno=74, function='<lambda>', code_context=['lambda_func = lambda : inspect.stack()[0]\n'], index=0)

But for example is there a way to get the lambda object field xxx in this case? For this the lambda object should be got somehow.

Carlos answered 14/1, 2022 at 18:11 Comment(9)
Can you give a minimal reproducible example of your lambda?Evince
Does this answer your question? How to get current function into a variable?Huda
Are you doing this just for learning, or do you have a practical use case?Sortition
@Huda The cited question is about how to get the object inside a normal function. This is my example code. But I would like to go a step forward to do similar inside a lambda function.Carlos
@Sortition Does it matter? :) Actually, I would like to get the function's property without referring to the name of the function. So, if I rename the function, I do not have consider change inside the function.Carlos
The PEP for this was specifically rejected. Just use a named function, and use the name.Diseased
self is just a parameter whose argument is set by the method instance that wraps the function used to define it. A lambda expression already defines a "normal function"; there is no fundamental difference between objects defined by def statements and objects defined by lambda expressions.Kally
@Diseased supports Monica Thx for the PEP! It is sad that this was refused. Actually (IMHO) it should be extended by lambda or function would do the job. For me it is strange that this was not implemented in the original version of the language...Carlos
@Kally You are absolutely right about this! Used as to show that there are some possibilities, but there are a lot of missing ones. But as the rejected PEP shows, it was not just my idea how this should work to be more complete.Carlos
P
2

We can now use a new python syntax to make it shorter and easier to read, without the need to define a new function for this purpose.

You can find two examples below:

Fibonacci:

(f:=lambda x: 1 if x <= 1 else f(x - 1) + f(x - 2))(5)

Factorial:

(f:=lambda x: 1 if x == 0 else x*f(x - 1))(5)

We use := to name the lambda: use the name directly in the lambda itself and call it right away as an anonymous function.

So in your particular use-case it would give something like that:

print((f:=lambda: f.__hash__())())  # prints the hash for example

You can do whatever you want with that f variable now (inside the lambda).

But in fact, if you don't mind multi-lines for your code, you could also just use the name directly and do something like that:

f = lambda : f.xxx
f.xxx = 2
print(f())

(see https://www.python.org/dev/peps/pep-0572 for more information about this := operator)

Pinnatipartite answered 17/1, 2022 at 17:32 Comment(1)
This is not a duplicate answer! I added a lot more details and explained the specific use case of the user. I just use two examples coming from another question here: https://mcmap.net/q/236653/-can-a-lambda-function-call-itself-recursively-in-pythonPinnatipartite

© 2022 - 2024 — McMap. All rights reserved.