5 ms·
Does Lua have "crappy scoping"? My understanding is that it has full lexical scoping. And somebody normally liking Python is in no position to complain about ba
by tikhonj 14y ago
Does Lua have "crappy scoping"? My understanding is that it has full lexical scoping. And somebody normally liking Python is in no position to complain about bad scoping rules!
I haven't used Lua myself, but I'm impressed by the design: it seems like an elegant version of JavaScript minus most of the really obnoxious JavaScript warts.
The tables and metatables seem particularly great: they look like a simple, elegant version of prototype-based inheritance without some of the problems JavaScript suffers. It's certainly simpler and more expressive than a class-based system.
Also, Lua is well known for having coroutines that are done correctly. That's probably not immediately useful for complete beginners, but it's certainly a nice feature when you get to it.
- Avshalom 14y agoIt does have full lexical scoping, however "global by default" is good for scripting, questionable for standalone programming. I general yes Lua is a great language, I have a lot of fun writing in it. But it does have its warts (though fewer than Javascript).
- ricardobeat 14y agoHave you heard of moonscript[1]? It is a bit quirky but makes var declarations local by default. [1] http://moonscript.org/ http://moonscript.org/
- brianto2010 14y agoFor what it's worth, you can use the local keyword to limit the scope of variables. http://www.lua.org/pil/4.2.html http://www.lua.org/pil/4.2.html
- deleted 14y ago[deleted]
- justincormack 14y agoNot a python expert, but python3 has had to add unlocal to let you create a non local variable. Lua requires explicit local, else things are global (which has different problems but seems a more logical choice). Lua also makes all loop indexes local to the loop which is very clean. Python has difficulty with this.
- tikhonj 14y agoBasically, I personally take issue with the way Python handles global variables and, until Python 3, completely fails to handle non-local variables. Let's take a trivial example that would work in JavaScript or Scheme. Often, I want to keep track of how many times a particular function gets called (usually for debugging). I would expect to be able to do something like this: counter = 0 def foo(bar, baz): counter += 1 ... This does not work in Python. You could use the global keyword to work around this: counter = 0 def foo(bar, baz): global counter counter += 1 ... This is something of an ugly hack in the language design. Moreover, this falls apart if counter is not in the global scope itself--for example, if foo is defined inside another function. Python 3 introduces the nonlocal keyword to deal with this. Fundamentally, this problem stems from the fact that Python conflates two fundamentally different operations: defining a variable and mutating it. I personally really like how Scheme handles this: defining and setting are two different actions, and trying to set a variable that has not been defined is an error. This makes the intentions of the programmer much clearer. A very common pattern in Scheme and JavaScript (as well as other languages I don't use, no doubt) is to use closures to encapsulate state. Thanks to Python's scoping model, this is at best a pain and certainly unnatural. There are also some minor annoyances. One that gave me problems recently (although it has been fixed in Python 3, I believe), was with list comprehensions: [i * i for i in range(1, 10)] print i # gives you 9!
- Xion 14y ago> A very common pattern in Scheme and JavaScript (...) is to use closures to encapsulate state. Thanks to Python's scoping model, this is at best a pain and certainly unnatural. It is also unnecessary. Python has normal classes, so you can contain your state within objects rather than stackframes.
- agscala 14y agoI don't like either of your examples. Your first example should have been a generator and used a yield rather than a global counter. Your second example is simply bad practice (what do you even expect to happen? It's ambiguous, but i having the value 9 seems to make a reasonable amount of sense).
- snarfy 14y agoI had learned Lua a few years before learning JavaScript. While learning JavaScript, I always felt like it was a dirty, warty version of Lua.
- ZoFreX 14y agoAnother area where Lua excels is interacting with C code. It's very easy to bind, and it runs really fast to boot, making it perfect for a scripting language in an engine that uses C for the heavy lifting.