[Python-ideas] Inline Functions - idea

Chris Angelico rosuav at gmail.com
Thu Feb 6 01:36:52 CET 2014


On Thu, Feb 6, 2014 at 11:26 AM, Andrew Barnert <abarnert at yahoo.com> wrote:
> 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.

Ah, possibly. I've never dug into that side of things, but that sounds
reasonable. It'd solve the indentation issues, at least. In any case,
it wouldn't be a function any more; it'd have to be a compile-time
alteration. You can't, at run time, choose between several such macros
- imagine if they differed in what names they made local.

ChrisA


More information about the Python-ideas mailing list