lua-users home
lua-l archive

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


On Mon, Aug 28, 2000 at 02:08:45PM -0300, tomas@ccpa.puc-rio.br wrote:

> > def f():
> >     global i
> >     i = i+1
> > 
> > Quite the opposite of Lua, I guess. :)
> 	How do you write a recursive call ?  Hope you don't have to
> define f as global!

No. The scoping rules of Python and Lua, in fact, seem to be very
similar, but the defaults are different. The rules, in a nutshell
are:

If you assign to a variable anywhere in a scope that is not top-level,
the variable is local everywhere in that scope, unless you declare it
explicity as global.

If you assign to a variable in the top-level scope, it is global.

Incidentally, the scoping rules of both Lua and Python also mean that
defining recursive local functions is not straightforward, because the
variable holding the function object is not visible to the function!

In Python you can pass the function object as a default parameter to
work around this problem. However, I have no idea how to work around
this in Lua. Upvalues don't work, because the variable is created
after the function definition has been evaluated. Just for the fun of
it, ideas anyone?

- Christian