Hacker News new | ask | show | jobs
by zahlman 525 days ago
(This sort of thing has been done before, and of course it will never make its way into the official Python distribution; but it's always fun to see.)

>Is it possible to do something similar with a decorator instead of a herestring?

No, this is a fundamental change to Python syntax. To apply a decorator to existing Python code, the decorated code has to compile first, creating a Python object (generally either a function object or "type" i.e. class object) which is passed to the decorator (which is really just a higher-order function with special syntactic support).

But of course, you could write the code to preprocess in a separate file with a different extension, and then have actual Python code which reads and preprocesses it and then evals the result. (The internals could also be written differently, in terms of AST manipulations using the "compiler services" part of the standard library. But this way is probably easier.)

On Linux, you could also have a top-level script which does those steps for a specified input filename, and then specify that script in the shebang line for the Dopy code file.