[Python-ideas] Inline Functions - idea
Haoyi Li
haoyi.sg at gmail.com
Thu Feb 6 02:06:19 CET 2014
> Which has already been implemented very nicely by Li Haoyi--see MacroPy
on PyPI.
In fact, my *trace* and *require*
macros<https://github.com/lihaoyi/macropy#tracing>do exactly what
we're describing here: they expand and grab whatever
*log* function is present in the scope they're called and use it to do the
logging. That way you can control what you're using for logging without
having to pass it everywhere.
> You can't, at run time, choose between several such macros - imagine if
they differed in what names they made local.
You probably don't want to! We're talking about functions that grab *different
things from their enclosing scope* *depending on how they feel like when
you run them*. This is the kind of thing that makes your colleagues go to
your house and splash paint on your car.
> Even when you're using "format_map"?
With macros, you get string
interpolation<https://github.com/lihaoyi/macropy#string-interpolation>by
grabbing them statically at load-time without having to do all this
load
time magic.
As Guido said, dynamic locals() is hard to do for the compiler. If you ever
find yourself writing a JIT like PyPy, you will curse the presence of
locals() for the headache they cause you when you're trying to optimize
away all the silly dictionary lookups.
In general, an unhygienic macro is exactly an inline function. It takes its
arguments at load-time and splices in the resultant AST into the call-site.
You should go have a go with them =)
On Wed, Feb 5, 2014 at 4:49 PM, Guido van Rossum <guido at python.org> wrote:
> On Wed, Feb 5, 2014 at 4:37 PM, MRAB <python at mrabarnett.plus.com> wrote:
>
>> On 2014-02-06 00:15, Guido van Rossum wrote:
>>
>>> On Wed, Feb 5, 2014 at 2:26 PM, Ethan Furman <ethan at stoneleaf.us
>>> <mailto:ethan at stoneleaf.us>> wrote:
>>>
>>> Possibly, but it's easy enough to simulate (at least in a read-only
>>> fashion) with **locals().
>>>
>>>
>>> Eew. Use of locals() for any purpose other than debugging should be
>>> discouraged.
>>>
>>> Even when you're using "format_map"?
>>
>
> Yes. Explicit is better than implicit. locals() is hard for the compiler
> to get right -- I wouldn't be surprised if some compilers would have to
> generate less optimal code if locals() is used. (Similar to
> sys._getframe().)
>
> --
> --Guido van Rossum (python.org/~guido)
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140205/49955a48/attachment.html>
More information about the Python-ideas
mailing list