[Python-ideas] Inline Functions - idea

Andrew Barnert abarnert at yahoo.com
Thu Feb 6 01:26:59 CET 2014


On Feb 5, 2014, at 7:28, Chris Angelico <rosuav at gmail.com> wrote:

> On Thu, Feb 6, 2014 at 2:15 AM, Alex Rodrigues <lemiant at hotmail.com> wrote:

>> The remaining use cases for inline functions are times when you would need
>> the same capabilities in multiple functions (calling in multiple methods of
>> a class for example), or when you wish to define what is essentially a
>> closure at the first level of execution, since closures are not possible
>> when you are not in a function.
> 
> The nearest concept I can think of to your proposed inline functions
> is a preprocessor macro. Effectively, you create a simple token that
> expands to a block of source code, and it's handled prior to the
> parsing and compilation of the code. There are times when that sort of
> thing is really useful, but it's oh so easy to make horribly
> unreadable code. (Plus, it'd be hard to get indentation right, if the
> different contexts are indented to different levels.)

Higher-level macros are a much better match. In lisp terms, what you want is a non-hygienic (or optionally-hygienic) syntactic macro. In Python terms, you can do that by working on ASTs rather than source code. Which has already been implemented very nicely by Li Haoyi--see MacroPy on PyPI.


More information about the Python-ideas mailing list