lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

----- Original Message -----
From: "John D. Ramsdell" <>
| I'm quite puzzled by the description of Python's new scoping rule,

You're not the only one!

| but here is my best guess as to what it is saying: A local variable that
| is referenced by one or more functions defined within the block in
| which the local variable is defined cannot be changed.  In Lua terms:
| the following code is illegal because local a cannot be modified since
| it is referenced by the function bound to foo:
|     do
|         local a = 5
|         local foo = function() print(a) end
|         a = 10   -- Illegal under Python rules!
|         foo()
|     end
| Even though this rule is more restrictive that Lua's rule, Python DOES
| provide lexical scoping as functions defined within a block have
| access to all the bindings made in the block.

It does say that it was Guidos (none too popular) decision to do this. The
workaround is to put the value of a in a container table and access this. I
think this is what John Belmonte does?

         local a = {5}
         local foo = function() print(%a[1]) end
         a[1] = 10   -- Now legal for Python!

Since the "global" keyword is to be added in v4.2(?) what about a "static"
keyword so you could do lexical scoping? (as you suggest)

  function a()
    local b = 1
    static c = 2
    local d = function()
      print(b) -- cant do
      print(c) -- can do

Or: why not retain upvalues for fast immediate outer scope access and instead of
generating an error for locals not found perform a lexical scope search then.
This would be compatible with current functioning code (I think). Then you have
the option.