3 ms·
In my experience, most uses for dynamism in Python are concentrated at initialization time. Features that make dynamic changes like namedtuple or decorators are
by yorwba 9y ago
In my experience, most uses for dynamism in Python are concentrated at initialization time. Features that make dynamic changes like namedtuple or decorators are usually only executed once when a module is imported and essentially implement a kind of code-generation that directly creates the intended objects.
I wonder whether it might be feasible to import a Python module (running all the initialization code) and then walk the reachable object graph to serialize it into code in a more static subset.
- chubot 9y agoYes absolutely! I've long been interested in multi-stage programming for this reason. Oil is filled with this pattern: do a bunch of metaprogramming at startup to make some data. Then use that immutable data for the rest of the program. There are very much two stages. I think Lua-Terra might be closest to the thing I want, although I haven't had a chance to play with it: http://terralang.org/ http://terralang.org/ I mention Bob Nystrom's language Magpie, which has an interesting model. Do the type checking at main(), not after parsing! But everything that happens before main(), at import time, is metaprogramming! A Problem with Type Checking http://www.oilshell.org/blog/2016/11/30.html http://www.oilshell.org/blog/2016/11/30.html I think I want to do compilation/optimization right before main(), not just type checking. It's all a bit vague right now, but I think OPy can go in this direction. Having the compiler written in its own language facilitates this. You can run code first, and then compile. This post is also related: Type Checking vs. Metaprogramming; ML vs. Lisp http://www.oilshell.org/blog/2016/12/05.html http://www.oilshell.org/blog/2016/12/05.html Also note that C++ is a two-stage language too. In fact Herb Sutter just proposed that they unify the two languages. Like you can use STL with constexpr at compile time. Link on this page: https://github.com/oilshell/oil/wiki/Metaprogramming https://github.com/oilshell/oil/wiki/Metaprogramming I think your suggestion is exactly what I've been thinking, so if you want to talk more about it / work on it, feel free to mail me :) The code needs a bit of work but I think it's a promising direction.