Comment to “A Look at Lua”

In the June issue of Linux Journal (#158) there’s an article on Lua in which the author compares it to Python. The article is only available to subscribers at the moment, so I decided to post my comment here as well.

  1. “There is no need to worry about different types of integers.” There is non need to do that in Python either nowadays. The conversion between “regular integers” and long ones is done automagically. Take the following function:

    def frac(n):
        if n == 0: return 1
        else: return n * frac(n - 1)
    

    Evaluating frac(10) gives the result 3628800 while frac(30) returns 265252859812191058636308480000000L.

  2. “You can add a float, long integer or a double to any other type of integer or number without a hitch in Lua. In contrast, doing this can cause programs written in Python 2.4 or older versions to crash.” Really? I’ve never heard of this problem. Do you have any references?

  3. “In contrast to Python, Lua does not focus on 100% backward compatibility.” That Python would focus on 100% backward compatibility is just plain wrong. Code I’ve written broke when moving from version 2.4 to 2.5 due to changes in generators. This is the only time I’ve personally been bitten by it but every version of Python has broken something from earlier versions. AFAIU Python 3000 will break a whole lot of older Python programs.

  4. “Unlike Python, global variables do not need to be declared.” They don’t need to be declared at all, not on a global level. If a function wants to access a global variable then it has to declare it as global. Contrast that to the following point.

  5. “Exactly the opposite of Python, most variables in Lua are global by default, and you must declare the variable “local” to make it a local variable rather than assuming that all variables are local.” Variables in Python are local by default, which in my experience better reflect how variables are used by programmers.

  6. “The for loop is a little more complex in Python than it is in Lua, because Python needs to know the key to be able to get the key’s value.” It’s possible to iterate over both key and value in Python as well:

    h = {1:'hello', 2:'world'}
    for k, v in h.iteritems():
        print k, v
    

In general it’s a good article, it shows off some interesting points in Lua. In order to make a comparison between two languages it’s necessary to have very good knowledge of both languages, otherwise there’s a real risk that one language is the favourite simply because the author knows that one the best. This piece definitely suffers from that.

I believe it would have been better without the author’s personal opinions on aesthetics sprinkled across the piece.

Share

2 Comments

  1. Regarding point #4, in python you only need to declare a variable as global when you want to assign it in a local scope. If all you do is read it, no special declaration is needed.

  2. Daniel, yes, you are right. I was slightly confused in my testing on this since I mistakenly thought that accessing the variable before assigning it would be allowed as well. Thanks adding that clarification!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>