Skip to content
Advertisement

Accessing __doc__ of function inside a lambda

I would like to extract the docstring of a function once it has been wrapped in lambda.

Consider the following example:

JavaScript

I get:

JavaScript

How can I reference the function called on “calling” the lambda one?

Update

Thanks for all answers:

JavaScript
JavaScript

Advertisement

Answer

There is no “good” way to do this. However, it is technically possible using the inspect module. Here is a very brittle and fragile implementation that fits your use case of getting the docstring of the first function called by a lambda:

JavaScript

As I said, this implementation is fragile and brittle. For instance, it breaks instantly if the first function called is not in globals. But if you find yourself in very dire straits, you can probably hack together a solution for your use case.

If at all possible, however, you should use functools instead

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement