Creating functions programmatically
Anders J. Munch
andersjm at dancontrol.dk
Thu Dec 12 03:31:32 EST 2002
"Martin v. Löwis" <martin at v.loewis.de> wrote:
>
> > But perhaps someone can point me to a better way? I am slightly
> > overwhelmed by the many options python has to offer for dealing with
> > python code (compiler, compiler.{pycodegen,symbols,syntax,..}, new,
> > ast, code, py_compile, compile, exec, eval, ...).
>
> I recommend to ignore most of these. Each of them was created to scratch
> someone's itch, and then turned out not to be applicable for a more
> general, or slightly different use case.
So I've noticed. In Python 3000 I expect tokenize->ast->compile->new
will form a coherent string where output from one module will be
usable as input to the next.
> > One thing that I don't see how to achieve with exec is control over
> > location in diagnostic messages. Regular Python diagnostics are fine,
> > but file and line should not point to the exec statement in the .py
> > file, but to the appropriate place in the file I'm parsing. I could
> > use something akin to C's #line here, is there any Python equivalent?
>
> Unfortunately, no. *That* is something that the compiler package should
> help to implement: the compiler package is supposed to mimic the builtin
> compile function 1:1, but it is pure Python. and provides various hooks
> (usually by allowing you to inherit from some class).
Is the compiler package a work in progress? The 2.2.2 documentation
lists 8 different submodules yet describes only .ast and .visitor, so
I figure it must be a fairly recent development.
Thanks for the input,
Anders
More information about the Python-list
mailing list