Getting in to metaprogramming

Aaron Brady castironpi at gmail.com
Tue Nov 25 16:35:59 EST 2008


On Nov 25, 5:20 am, Michele Simionato <michele.simion... at gmail.com>
wrote:
> On Nov 25, 12:12 pm, Rafe <rafesa... at gmail.com> wrote:
>
> > is it really as simple as gathering strings of code?
>
> Yes.

Yes.

> > Sort of like generating HTML or XML directly? Is there any other framework or
> > pattern set that is worth looking in to?
>
> Yes, the compiler module and the ast module in the standard library.
> You may also
> look at how templating languages that compile to Python code work (I
> mean mako
> or PTL/qpy). You may find interesting how the import hooks work too.

You could have Python generate HTML, Python generate COBOL, Python
generate Python, Perl generate Python, sure.  Not that I know COBOL.

If you're generating Python with Python, you only need one file to do
it, not two separate programs, as you would for the other
combinations; it was my point.  It could be really helpful or
unnecessary, depending on your application.

There are some templating modules, possibly for Python code.  If
anyone knows them, they can point them out.

I don't know a clean, reliable way to structure a metaprogram though.
Mine always turn out messy.



More information about the Python-list mailing list