Ah. It wasn't clear to me from the thread that James was using `inspect`. As it happens, not only does getsource give more than it should, it also gives less than it should. The following bug still exists in 3.6.1. It was closed as a wontfix bug back in Python 2 because, I presume, fixing it would require changing code objects. https://bugs.python.org/issue17631
Regardless, I think it'd be better not to rely on such magic for the proposal. While it's a fun puzzle, whether it's possible or not, it still modifies the syntax and/or scoping and/or evaluation order rules of Python. On Tue, Sep 25, 2018 at 1:54 PM Marko Ristin-Kaufmann <marko.ris...@gmail.com> wrote: > > Hi James and Franklin, > > getsource() definitely does not work. I tried for a long, long time to make > it work and finally gave up. I parse in icontract the whole file where the > lambda function resides and use asttokens to locate the node of the lambda > (along some tree traversing upwards and making assumptions where the > condition lambda lives). > > Have a look at: > > https://github.com/Parquery/icontract/blob/391d43005287831892b19dfdcbcfd3d48662c638/icontract/represent.py#L309 > > and > https://github.com/Parquery/icontract/blob/391d43005287831892b19dfdcbcfd3d48662c638/icontract/represent.py#L157 > > > On Tue, 25 Sep 2018 at 19:48, James Lu <jam...@gmail.com> wrote: >> >> > I'm surprised you haven't found >> > inspect.getsource(func) >> >> I did. That’s exactly what I was describing in the paragraph. It wouldn’t >> work in interactive mode and it includes everything on the same line of the >> lambda definition. >> _______________________________________________ >> Python-ideas mailing list >> Python-ideas@python.org >> https://mail.python.org/mailman/listinfo/python-ideas >> Code of Conduct: http://python.org/psf/codeofconduct/ _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/