[Python-ideas] "old" values in postconditions

Franklin? Lee leewangzhong+python at gmail.com
Tue Sep 25 13:27:10 EDT 2018


Ew, magic. `{{foo}}` is already valid syntax (though it will always
fail). I don't like this path. If the proposal requires new syntax or
magic, it will be less likely to get accepted or even pip'd. Remember
also that PyPy, IronPython, and Jython are still alive, and the latter
two are still aiming for Python 3 (PyPy 3 is already available).

On Tue, Sep 25, 2018 at 12:52 PM James Lu <jamtlu at gmail.com> wrote:
>
> Hmm, I was wrong: there is no reliable way to get the code of a lambda function.

You may be looking for inspect.signature(func).

> Or maybe we could send a PEP to add the .func_code attribute to lambdas as well as normal functions.

The new name is `.__code__`. There is some documentation of code
objects in the `inspect` documentation (names beginning with `co_`).
More documentation:
https://docs.python.org/3.7/reference/datamodel.html#index-55

> There’s also a technique online where they find the lambda’s source by locating the file the function was defined in and then removing the irrelevant parts, but that just doesn’t sound practical to me.

I'm surprised you haven't found
    inspect.getsource(func)

Doesn't always work if the source is not associated to the definition
of func, such as with functions defined in C. That shouldn't be a
problem with lambdas. You might need to worry about file permission. I
don't know if other interpreters will find it difficult to support
`inspect`.

You may be looking for `ast.parse(inspect.getsource(func))`.


More information about the Python-ideas mailing list