PEP 309 - Built-in closure type (with tentative syntax proposal)

Christos TZOTZIOY Georgiou DLNXPEGFQVEB at spammotel.com
Wed Feb 12 09:22:47 EST 2003


On Wed, 12 Feb 2003 07:01:31 +0200, rumours say that Beni Cherniavsky
<cben at techunix.technion.ac.il> might have written:

>> Hm... perhaps static variables would be something more generic, a la:
>>
>> def function(args):
>>     static:
>>         c = 5
>>         d = 7
>>     # normal code follows here
>>
>Please don't.  Not like this.  The proposed syntax means that the current
>assumption "a def's body is not executed at definition time" breaks and I
>need to start inspecting all function bodies for ``static``.

Nope, you seem you didn't read my post.  You also didn't quote the
following from my text:

>>Everything in the static block would be executed only the first time the
>>function is called, 

I didn't describe a mechanism, but I believe I was clear: "the first
time the function is called", not "when the function is defined".
Perhaps the mechanism will take another opcode, or it could be simulated
with a test of a hidden (inaccessible by python code) boolean object
whose reference was kept in the freevars array a la:

static:
   ...

means

if <the_hidden_var_in_closure>:
   <the_hidden_var_in_closure>= False
   ...

>  def closure:    # back to the subject :-)
>    c = 5
>    d = 7
>    def function(args):
>      "normal code follows here"
>    return function
>  function = closure()

This is the way to create closures and I use it too.  Can you rebind c
or d inside function?  If yes, please show me how.

After all, I won't write a PEP unless I have a working patch (I think
it's a prerequisite for a PEP).
-- 
TZOTZIOY, I speak England very best,
Real email address: 'dHpvdEBzaWwtdGVjLmdy\n'.decode('base64')




More information about the Python-list mailing list