lunatic-python icon indicating copy to clipboard operation
lunatic-python copied to clipboard

pass python dictionary to lua is a userdata format, how to use it?

Open shimiaoyuan opened this issue 9 years ago • 1 comments

shimiaoyuan avatar Jul 05 '17 03:07 shimiaoyuan

To use methods in a dict object from lua you need to access it as attributes. The two ways of doing that:

  • use asattr, for example:

     local py = require 'python'
     local dict = py.builtins().dict
    
     local d = dict{foo = 1, bar = 2}
     local d_attr = py.asattr(d)
    
     print(d_attr.items())
     d.baz = 5
     print(d_attr.items())
    
  • use python.builtins().getattr():

     -- continuing from above
     local getattr = py.builtins().getattr
     print(getattr(d, 'items')())
    

greatwolf avatar Jul 05 '17 08:07 greatwolf