Search code examples
pythonluaintrospection

Python has dir(obj) and help(obj), is there an equivalent in Lua?


For any one unfamiliar with those two methods in Python...

dir returns a list of methods/properties of a specific object.

help returns an objects doc strings.


Solution

  • Lua has no concept of "doc strings."

    All Lua objects are tables (or userdata, but you can't inspect userdata from Lua code. Well not in Lua 5.1; in 5.2, you can do it if the user who created the userdata wants you to). Therefore, you can just use existing table methods to iterate over the contents of a table:

    for k, v in pairs(obj) do
      --Do stuff with k(ey) and v(alue)
    end
    

    Note that this will invoke metamethods, so the true contents of the table aren't known. Then again, if the person who created that object wanted you to have the true contents, he wouldn't have protected them with metamethods.