3 ms·
Python has way more metaprogramming capabilities than people think. Of course you can intercept pretty much anything with __dunder__methods (attribute missing,
by sametmax 7y ago
Python has way more metaprogramming capabilities than people think.
Of course you can intercept pretty much anything with __dunder__methods (attribute missing, method access, instantiation, etc), you have full instrospection of pretty much anything (functions, objects, modules, call stacks...) and you also have monkey patching, decorators, metaclasses, bytecode injection...
But while we are far away from Lisp based languages, import hooks give you access to the ast of any module, and allow to use your own parser, before the result is loaded, to decide what to return. Which means you can pretty much make your own syntax and import it like a regular module.
However, the reason those tools are not very well known is that the community regards magic as dangerous as it is powerful, and everybody agrees on using it sparingly.
Hence the only popular libs that use a lot of magic are ORM (lot of metaclasses, dunders, etc) and test libs (monkey patching, bytecode injection, ast parsing), but not a lot more. I don't know of any popular lib that actually uses the import hooks to create a DSL.
- mark_l_watson 7y agoThe Hy language (HyLang) is a good example of this. A lisp syntax to transformed to an AST that is executed as Python is. Hy is an interesting project and a good bet if you really want to use Keras and TensorFlow in a Lisp language.