In the following example code, inspect.getsource(lambda_argument)
seems to include the source code of the function call surrounding the definition of the lambda. How can I prevent that?
import inspect
def fun_of_lambda(lambda_argument):
print(inspect.getsource(lambda_argument))
fun_of_lambda(lambda x, y, z: x + y + z)
Output:
fun_of_lambda(lambda x, y, z: x + y + z)
Previous version of the question:
import inspect
def decorator_with_lambda(lambda_argument):
def decorator(inner_fun):
print("Printing lambda_argument ...")
print(inspect.getsource(lambda_argument))
return inner_fun
return decorator
@decorator_with_lambda(lambda: True)
def function() -> None:
pass # This should not be printed as part of lambda_argument!
function()
Output:
Printing lambda_argument ...
@decorator_with_lambda(lambda: True)
def function() -> None:
pass # This should not be printed as part of lambda_argument!
Update: dill.source
seems to have the same issue, and I have reported https://github.com/uqfoundation/dill/issues/583.