简体   繁体   English

如何从Python中获取lua /翻译中的类类型

[英]how to get the class type in lua / translation from python

I am trying to find something analogous to the class in lua. 我正在尝试找到与lua类相似的东西。 In python, I would do: 在python中,我会这样做:

a = {}
type(a)
>>>> dict

So I have the object vocab in lua. 所以我在lua中有对象vocab。 When I print the object, I get: 当我打印对象时,我得到:

print(vocab)
>>> {
3 : 20
o : 72
m : 70
d : 61
( : 9
}

how can I get lua to spit the object, something analogous to the type() in python? 我怎样才能得到lua吐对象,类似于python中的type()? - which will give you the class of the object -这将为您提供对象的类别

There are 8 types in Lua : nil, boolean, number, string, function, thread, table and userdata . Lua有8种类型 :nil,布尔值,数字,字符串,函数,线程, userdata You can find out which of these basic types your object belongs to using built-in type() function: 您可以使用内置的type()函数找出您的对象属于哪些基本类型:

type('Hello world')                    == 'string'
type(3.14)                             == 'number'
type(true)                             == 'boolean'
type(nil)                              == 'nil'
type(print)                            == 'function'
type(coroutine.create(function() end)) == 'thread'
type({})                               == 'table'
type(torch.Tensor())                   == 'userdata'

Note that the type of torch.Tensor is userdata. 请注意, torch.Tensor的类型是userdata。 That makes sense since torch library is written in C. 这是有道理的,因为torch库是用C编写的。

The type userdata is provided to allow arbitrary C data to be stored in Lua variables. 提供了userdata类型,以允许将任意C数据存储在Lua变量中。 A userdata value is a pointer to a block of raw memory. userdata值是指向原始内存块的指针。 Userdata has no predefined operations in Lua, except assignment and identity test. 除了赋值和身份测试外,Userdata在Lua中没有预定义的操作。

The metatable for the userdata is put in the registry, and the __index field points to the table of methods so that the object:method() syntax will work. 用户数据的元表放在注册表中,并且__index字段指向方法表,以便object:method()语法起作用。

So, dealing with a userdata object, we do not know what it is but have a list of its methods and can invoke them. 因此,在处理userdata对象时,我们不知道它是什么,但是拥有其方法列表并可以调用它们。

It would be great if custom objects had some mechanism (a method or something) to see their custom types. 如果自定义对象具有某种机制(一种方法或某种方法)来查看其自定义类型,那将是很好的。 And guess what? 你猜怎么着? Torch objects have one: 火炬对象有一个:

t = torch.Tensor()
type(t)       == 'userdata' # Because the class was written in C
torch.type(t) == 'torch.DoubleTensor'
# or
t:type()      == 'torch.DoubleTensor'

Speaking of Torch . 说到Torch It has its own object system emulator, and you are free to create some torch classes yourself and check their types the same way. 它有自己的对象系统仿真器,您可以自己创建一些火炬类并以相同的方式检查其类型。 For Lua , however, such classes/objects are nothing more than ordinary tables. 但是,对于Lua而言,此类/对象仅是普通表。

local A = torch.class('ClassA')
function A:__init(val)
    self.val = val
end

local B, parent = torch.class('ClassB', 'ClassA')
function B:__init(val)
    parent.__init(self, val)
end

b = ClassB(5)
type(b)       == 'table' # Because the class was written in Lua
torch.type(b) == 'ClassB'
b:type() # exception; Custom Torch classes have no :type() method by defauld

It is said that Lua defines "mechanisms, not policies". 据说Lua定义了“机制,而不是政策”。 Classes would be a "policy". 上课将是一种“政策”。 You can implement the effect of classes however you like; 您可以根据自己的喜好实现类的效果。 Lua provides many mechanisms that could be used to do so. Lua提供了许多可用于实现此目的的机制。 But Lua has no single way of declaring such a construct. 但是Lua没有声明这种构造的单一方法。

Lua's type standard method will only return the broad category of a Lua value: number, string, table, etc. And since Lua has only one kind of data structure (table), every kind of class (not generated from C) would be of the "table" type. Lua的type标准方法将仅返回Lua值的广义类别:数字,字符串,表等。由于Lua只有一种数据结构(表),因此每种类型的类(不是从C生成)都是“表格”类型。

This has the side effect of making it difficult in Lua to use someone else's policies. 这具有使Lua难以使用其他人的策略的副作用。 For example, if Torch has a way to distinguish its own "classes" from regular tables, that would work. 例如,如果Torch能够将其自己的“类”与常规表区分开,那将是可行的。 But it wouldn't be able to distinguish some other class implementation from a regular table. 但这无法将其他类的实现与常规表区分开。

So there's no general way to distinguish a table from some form of "class". 因此,没有一般的方法可以将表格与某种形式的“类”区分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM