|
|
|
|
|
by quantumtremor
3628 days ago
|
|
So why not accept a lambda? I saw that http://stackoverflow.com/questions/334851/print-the-code-whi... gives you the source, though I'm not sure if it'll always work. This does work though: ~ λ echo "a = lambda x: x * 2" > test.py
~ λ py -i test.py
IPython 4.2.0 -- An enhanced Interactive Python.
In [1]: a(2)
Out[1]: 4
In [2]: import inspect
In [3]: inspect.getsource(a)
Out[3]: 'a = lambda x: x * 2\n'
I'm definitely a big fan of using Lisp's quoting for code-as-data, but I feel stringifying it makes the problem even worse. |
|
One other place I've seen this done is in the numexpr for Python.
https://github.com/pydata/numexpr
It does seem like this
could be The lambda is never executed because it compiles to machine code and not Python byte code, but that shouldn't make a difference. You should still be able to use the AST of the body as input to the compiler.And then a and b have to be pulled out of locals() automatically or something.