[Python-Dev] thunks

Oren Tirosh oren-py-d@hishome.net
Mon, 3 Feb 2003 12:49:13 -0500


On Mon, Feb 03, 2003 at 11:53:50AM -0500, Guido van Rossum wrote:
> Thunk blending in:
> 
>   [<variable> =] <expression>:
>     <block>
> 
> Thunk introducing a new local scope:
> 
>   [<variable> =] <expression> lambda:
>      <block>
> 
> (Never mind the ugly things I've said about lambda in the past. :-)
> 
> I'm confident that the parser can distinguish these, and I'm confident
> that the scope blending can be implemented using nested-scope cells.

Why not treat the "blended" version as just another nested block, like
for, if, while etc? Thinking in terms of "thunks" is useful if you intend 
to keep a reference to this code that can be executed after the function 
containing it has exited. Is there any reason to use a *blended* block
for this purpose? A piece of code modifies local variables of a dead 
function? Just think how messy it's going to be if it modifies the locals 
of the function while it is still running. I think it's best not to have 
any reference to it or not make it an object at all.

See my previous posting on how this type of "blended blocks" can be
imlemented mostly by reusing the generator mechanisms. Using iterators
for the "with" statement may be a little ugly, but it shows how this
problem can be approached without thinking in thunks.

The non-blended version seems more suitable for defining things like 
functions, classes, properties, widgets etc. I would prefer for it to 
have a syntax more similar to functions or classes - a syntax that binds 
exactly one name in the scope containing it and uses the form

keyword-introducer NAME {other stuff}:
    suite

rather than assignment. I think it will be a good cue to the fact that
these blocks are definitions with their own scope, not control flow
structures that blend into their surroundings.

> 
> Adding formal parameters to the second case would go like this:
> 
>   [<variable> =] <expression> lambda <arguments>:
>      <block>

If a non-blended block wants to define multiple entry points it can just 
use functions, like methods in a class. This requires that this block be
executed immediately and the resulting dictionary stored for later
use. I think the remembering the convention for naming the functions in
this block is preferred to this mixed syntax.

thunk-you-very-much,

    Oren