News for hackers. Stuff that matters.
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