Closures python,scheme,ruby

Bengt Richter bokr at oz.net
Fri Jul 16 04:10:45 CEST 2004


On 15 Jul 2004 13:09:44 +0200, Jacek Generowicz <jacek.generowicz at cern.ch> wrote:
[...]
>This illustrates that the appearance of "a =" in a function body makes
>python believe that "a" is local. But what if you want the function to
>modify some global state? What if you want to modify the global "a" ?
>
I was going to pick a nit and point to a corner case where an "a = ..."
in the function body still allows^Hed a (also) to refer to a non-local, i.e.,


 >>> def fn():
 ...     a = a   # local a used to get bound to non-local a, IIRC
 ...     print a
 ...
 >>> import dis
 >>> dis.dis(fn)
   2           0 LOAD_FAST                0 (a)
               3 STORE_FAST               0 (a)

   3           6 LOAD_FAST                0 (a)
               9 PRINT_ITEM
              10 PRINT_NEWLINE
              11 LOAD_CONST               0 (None)
              14 RETURN_VALUE

but code generation seems to have changed, or I misremember.
Maybe the change is to make it absolutely consistent with determining
semantics by full function body lookahead (which incidentally also
rubs me the wrong way for discovering yields to change a function
to a generator). I prefer meaning to depend just on what's
been read so far, top to bottom, left to right, unless there is some
overriding reason not to do it that way (e.g. normal expression and
assignment evaluation order).

I'm still a big python fan though ;-)
[...]
>I happen to appreciate closures.
I do too. What if
    x <= expr
meant evaluate expr and then
find x (as if to do a read access) and rebind it, whatever name space it's found in?
(Might be dangerous if you include searching builtins though)
Hm, ... not sure about rebinding a binding discovered by attribute name access -- i.e.,
     x.y <= expr
might rebind a class variable somewhere or an instance variable, depending.
Don't know how useful that might be.

Regards,
Bengt Richter



More information about the Python-list mailing list