Expect like syntax in Python

Courageous jkraska at san.rr.com
Mon Feb 4 07:18:21 CET 2002

>What I really want is some way to get anonymous functions that can be passed
>as part of a list of paramaters and called at the appropriate time.  There
>doesn't appear to be any way to get that particular effect in Python.

Well, there is lambda. It's just not fully featured, and this because of
complications in the way that Python manages whitespace and how that interacts
with the parser. Attempt the following in Python:

	for i in range(0,3): j=i; k=i

Now attempt this:

	for i in range(0,3): k=i; for j in range(0,3): m=j

Python won't let you do it. And for good reason. The statement is completely
ambiguous. There are similar problems with anonymous function definition in
Python, although they're not insurmountable.

I'm working on something similar in a play language, using ANTLR's lexer
_multiplexing_ capability. Certain kinds of blocks know how to multiplex
back and forth between whitespace-sensitive and insensitive forms. Using
this technique, I'm able to easily do exactly what you describe.

IIRC, it looks like this:

	f ( y,
		code( x ):
			return x

Like Python (){}[] always imply no indent/dedent mechanics. Unlike Python,
the ':' block introduction multiplexes back to indent/dedent mode.

It works really well!

A more fun thing that I'm working on is a macro processor that is such that
the following could be a macros in the language:

	while [condition];

... or even...

	case [expression]:
	switch [expression]:
	switch [expression]:


More information about the Python-list mailing list