[Python-ideas] And now for something completely different

Cliff Wells cliff at develix.com
Thu Sep 18 00:41:27 CEST 2008


Okay, for anyone who's still willing to bear with me, I have a
completely different approach that might be more palatable (and perhaps
even appealing) to some people.

I was reading the documentation for Io ( http://www.iolanguage.com ),
particulary about how Io implements control structures: in short, it
doesn't.  What it's able to do is let the user define control structures
in Io itself. For example "if" and "for" are merely functions:

for ( i, 1, 10, i println )
if ( b == 0, c + 1, d )

Now, the reason this works in Io (and not in Python) is because while
you *can* implement a function that mimics the logic of, say, an
if-statement, there's no way to implement lazy evaluation of the
resulting values.  So for instance:

def IF ( condition, iftrue, iffalse ):
    if condition: return iftrue
    return iffalse

result = IF ( hasattr ( spam, 'eggs' ), spam.eggs, 'not found' )

The expression spam.eggs is evaluated regardless of the truthiness of
the condition (in fact, causes an exception), so this construct is
basically worthless.

So... now, here's my shiny new idea: lazy evaluation of function
arguments (by some explicit syntactical indicator).  Arguments would not
be evaluated until they are encountered *at runtime* in the function
body.  They could be compiled, but not evaluated.  

You can accomplish this to some degree by using lambda, but I'd much
prefer something indicated in the function signature than in the calling
code.  That is rather than:

def foo ( arg ):
    return arg ( )
foo ( lambda: x )

I'd prefer to write

def foo ( lambda: arg ):
    return arg # evaluation happens here
foo ( x )

("lambda" not necessarily being the actual name of the token in the
second case).

I think this would have appeal outside of my particular desire (lazy
evaluation is a useful feature in-and-of itself) but would also solve my
particular issue (I could implement functional versions of any control
structure I like, and even implement new ones).

Thoughts?

Regards,
Cliff





More information about the Python-ideas mailing list