Lua 中是否有类似于collections.defaultdict
Python 中可用的功能,它自动处理不存在的关联数组键的默认值?
我希望下面的代码设置nil
为v
而不是错误。所以基本上是一种a[2]
(不存在的键)table
默认情况下的方法:
a = {}
v = a[2][3]
>>> PANIC: unprotected error in call to Lua API (main.lua:603: attempt to index field '?' (a nil value))
在 Python 中可以这样完成:
>>> import collections
>>> a = collections.defaultdict(dict)
>>> print a[2]
{}