How to compile a multi-line function definition ?

Christian Tismer tismer at appliedbiometrics.com
Thu May 13 13:50:58 EDT 1999


Jerome ALET wrote:
> 
> Hi there,
> 
> I've just read the complete Python's documentation (well, almost :-) and
> I've found nothing to solve my problem. This doc is very good but lacks
> examples, IMHO.
> 
> Given the following sample of code which works fine in Python 1.5.2, I
> want to know how to define a multi-line "my_function". Every time I try
> something I get a syntaxerror or something similar.
> 
> -------- CUT ------------
> # mytest.py
> # module to do some testing
> class MyClass :
>         loc = locals()
>         def __init__(self) :
>                 exec(compile("def my_function(self, a, b) : print a, b,
> '<==>', b, a""", "<string>", "exec"), self.loc)
> ---------- CUT ----------

Your code string is missing a "\n" at its end.

BTW, what are you trying to achieve? It doesn't look like
the right path, I guess. Where do you want to put "my_function",
shall that become a new method for your instance?
Will not work, although there are ways to do this.

But before, I suggest to play more with classes and instances
in the way they are designed for, then you might be able to
avoid tricks like generating code.

cheers - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list