13 ms·
Import hooks are awesome, one of the cool things you can do is re-write the code as you load it. I made a toy loader that automagically inlines (some) Python fu
by orf 11y ago
Import hooks are awesome, one of the cool things you can do is re-write the code as you load it. I made a toy loader that automagically inlines (some) Python function calls[1]. There are also cool projects like MacroPy[2] that do much more extensive things.
1. https://tomforb.es/automatically-inline-python-function-calls https://tomforb.es/automatically-inline-python-function-call...
2. https://github.com/lihaoyi/macropy https://github.com/lihaoyi/macropy
- finyeates 11y agoAwesome, I saw your post a while back and it triggered me to write a full inliner for python, also added some IFDEF macros using context managers e.g. with IFDEF("DEBUG"): I'll have to dig around and find my code, your inline project was a great help in understanding the import hooks and walking the ast.
- dr_zoidberg 11y agoI read your experiment on inlining and it looks quite interesting. Did you do a benchmark on how it affects (improves?) performance? In my line of work I've found ocasionally places where inlining would've helped (functions that do masking with 64 bit masks, for example, are a mess to "inline by hand" and kill a lot of readability and clarity). Even with the current limitations of your "toy" implementation, it seems like it would help to avoid the costly function calls.
- jimbokun 11y agoSounds a bit like the Ruby-fication of Python.