code blocks in Python

Hung Jung Lu hungjunglu at yahoo.com
Sun Nov 23 19:34:39 EST 2003


hungjunglu at yahoo.com (Hung Jung Lu) wrote in message > 
> 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

It just occurred to me a better syntax:

def A:
    x = 1
    y = 2

def B:
    z = x + y
    print z

No new keyword needed, beside, it uses the same keyword as function
definition, so the purpose of the code block is clear: it's something
that you will invoke.

A proper name for this type of meta-programming is "Linear
Meta-programming". Just like in algebra you have linear algebra and
non-linear algebra, in meta-programming you have all sorts of
non-linear ways of taking code pieces and turn them into other code
pieces, these include e.g.: (a) compiling: turning a character string
into a binary code string, (b) encryption, (c) macro/token
substitution, (d) tweaking with the internals like meta-classes, etc.
Linear meta-programming is much more modest and well-organized. It's
like sequencing DNA bands. Each statement is like a DNA nucleotide,
each codeblock is like a DNA band. You just want to sequence the
codeblocks properly, and at times replace one block with some other
block, or insert additional blocks.

I still have yet to hear any difficulties/complaints.

I refuse to believe that Python developers have NEVER considered this
extension to the language. So, what's the problem?

Hung Jung




More information about the Python-list mailing list