lua-users home
lua-l archive

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

Marinho Brandao wrote:
> well... I tried your dir function with a table and it printed the keys
> and values for me (not the table's attributes and methods)

Those *are* the table's attributes and methods!

I think you may be misled by your Python experience; Lua is *not* object
oriented, but it is possible (and very easy) to build object oriented
systems with it. What you would consider to be methods are instead
*functions* stored in a table's keys, or accessable via the metatable,
which are then called in an object-oriented way.

  object:method(args...) precisely equivalent to:

  object.method(object, args...)

It's a difference in philosophy between Python and Lua; Python provides
a framework in which you build your application, and Lua provides the
tools with which you can then build your own framework.

> I mean: in Python, I use dir() to list all that an object has and this
> makes me knows that an object has the method that I need, and so on...
> this makes me gain hours of research, just using the intuition.

In general the best thing to do for this is to go and look at the
documentation. The Lua core language docs are very good. Add-on modules
documentation is less good, alas.

Actually finding out from looking at an object what methods it supports
is rather difficult because there may be code involved at any point. In
general, you can usually find them as follows:

- if it's a table, keys on the table are found first
- if getmetatable(object).__index is a table, you might find them there
- otherwise getmetatable(object).__index is probably a function which
returns a callable object on demand

This can be nested arbitrarily deep, just to make life more interesting.

┌─── ───── ─────
│ "I have always wished for my computer to be as easy to use as my
│ telephone; my wish has come true because I can no longer figure out
│ how to use my telephone." --- Bjarne Stroustrup

Attachment: signature.asc
Description: OpenPGP digital signature