News for hackers. Stuff that matters.
Re:Why Lua? by meric on Tuesday January 31, 2012 @05:00PM (#3535396)
In lua, the metatable is the killer feature that python doesn't have.
Re:Why Lua? by gregholmberg on Tuesday January 31, 2012 @05:28PM (#3535505)
When my main language was C, I used to feel that dynamically mutating or self-modifying data structures were somewhere between black magic and heresy.

I have found that being able to place arbitrary constraints on the way my programs store things in (seemingly simple) data structures has been very, very powerful.

Re:Why Lua? by sshumaker on Tuesday January 31, 2012 @06:47PM (#3535858)
metatables don't involve dynamically mutating / self-modifying data structures. They're more equivalent to javascript's prototype, except slightly more general-purpose. You can easily use them to build an OO system, among other things.
Re:Why Lua? by gregholmberg on Tuesday January 31, 2012 @10:01PM (#3536441)
Yes, I agree that Lua metatables / metamethods look and work a lot like JavaScript's prototype property.

I should have been more specific when mentioning self-modifying data structures and Lua metatables in the same post.

Consider: it is possible for a table to be its own metatable, and for that table to contain (pointers to) some functions that govern storing data in the table. For the non-Lua reader, these tied-to-an-event-on-a-table functions are called table-access metamethods [0], and they allow for some dangerous shenanigans.

If these particular metamethods knew how to add to, rearrange, or delete from the set of callable functions already stored in the table, the subsequent layout and contents of the table could change in a non-deterministic way.

Something that looks like a simple assignment/update (__newindex/__index, insert/update, or your favorite pair of terms)

  t[0] = "command phrase"
     x = t[0]
could trigger wholesale reorganization of the table, including deletion or addition of new metamethods.

[0] "Programming in Lua 1ed (Chapter 13. Metatables and Metamethods, 13.4 – Table-Access Metamethods)" http://www.lua.org/pil/13.4.html

edit: incomplete example

Re:Why Lua? by hythloday on Tuesday January 31, 2012 @06:05PM (#3535667)
Could you explain what you can accomplish with Lua metatables that you can't do with python __eg__ methods? I've only seen them used as a way to implement operator overloading.
Re:Why Lua? by meric on Tuesday January 31, 2012 @08:59PM (#3536272)
In addition to everything a python class can do, you can also use setmetatable() to modify the metatable of a table at runtime. A metatable is also just a table so you don't have to assign it to designated "metatables". You can for example even set the metatable of a table to itself.

The metatable is more general and conceptually simpler. It doesn't have to be the "class" of an "object", it can implement other patterns to OO without feeling weird, because the assumption that metatable is "Object Oriented" just isn't there.

Do you have python beginners touching metaclasses? I doubt it. Do you have lua beginners touching metatables? Certainly yes.

Re:Why Lua? by sshumaker on Tuesday January 31, 2012 @06:57PM (#3535902)
__index and __newindex are the key ones - effectively equivalent to method_missing in Ruby. __index gets called if you try to read a non-existent key, and __newindex gets called if you try to write to a non-existing key. You can use these methods to implement inheritance, proxies, getters/setters, all sorts of stuff.
Re:Why Lua? by silentbicycle on Wednesday February 01, 2012 @12:22AM (#3536828)
Yes, but in Python, you set them on every object. In Lua, you can set them once (in a prototype) and have each table share them (and possibly other methods, or a reference to another when lookup fails there). This makes certain behaviors (e.g. implementing your on object system) cheap and straightforward that would be impractical in Python.

Imagine if each Python object had one field that stored all the __blah hooks, and objects could share those. It's actually more powerful than that, but it's a good start.

Re:Why Lua? by hythloday on Tuesday January 31, 2012 @07:14PM (#3535940)
This sounds very much like Python's __getattribute__ [0] method. Is there a difference I'm not understanding?

[0] http://docs.python.org/reference/datamodel.html#object.__get...

Re:Why Lua? by sshumaker on Tuesday January 31, 2012 @08:12PM (#3536148)
I'm not that familiar with Python, but can you use __getattribute__ to provide methods as well? Using __index is the way you typically implement inheritance in Lua.
Re:Why Lua? by timtadh on Tuesday January 31, 2012 @09:36PM (#3536393)
yes.

    >>> class A(object):
    ...   def __getattribute__(self, key):
    ...     try: return object.__getattribute__(self, key)
    ...     except AttributeError: return lambda: 12
    ...
    >>> a = A()
    >>> a.asdf
    <function <lambda> at 0x7fbb8fb6f848>
    >>> a.asdf()
    12
Re:Why Lua? by corysama on Tuesday January 31, 2012 @06:21PM (#3535740)
Roll-your-own prototypical inheritance. I also vaguely recall rolling my own approximation to class inheritance long ago.
Re:Why Lua? by jemfinch on Tuesday January 31, 2012 @07:26PM (#3535983)
You can do that trivially with __*attr__ methods in Python. If you want something more structured, you can easily do it with a metaclass.