code blocks in Python

Hung Jung Lu hungjunglu at yahoo.com
Sat Nov 22 22:13:19 EST 2003


"John Roth" <newsgroups at jhrothjr.com> wrote in message news:<vrvjpt7fh4lob8 at news.supernews.com>...
> Just some random observations. First, I'm not at all clear on
> what you want it to do. Some examples would be helpful. There
> are entirely too many different ways I could interpret your reference
> to metaprogramming for me to understand the usage you have in
> mind.

codeblock A:
    x = 1
    y = 2

codeblock B:
    z = x + y
    print z

exec A
exec B

This will be equivalent to writing the program:

x = 1
y = 2
z = x + y
print z

> If you want it as a replacement for the badly crippled lambda statement,
> then I think there's quite a bit of interest. What's holding that up is
> syntax, and an appropriate syntax isn't entirely obvious.

Lambda is a totally different thing. Lambda is an anonymous
*function*. I emphasize *function* because functions take parameters
and return values. Code blocks are, well, blocks of code. No
parameters, no nothing else. Simple and plain blocks of code. Once you
have codeblocks, you wire them anyway you want. Think of them as
macros in C++, only that in Python you can wire them dynamically
during runtime. Python already does that. It's just that you need the
compile() function, your code string is not compiled until the
compile() function is executed, and the resulting object type is
"Code" (which is a fine name, even if the new keyword were
"codeblock".) Have you tried the compile() function?

regards,

Hung Jung




More information about the Python-list mailing list