Owlready2动态class代

Owlready2 dynamic class generation

我正在尝试为 owlready2 ontology 动态创建 class。该文档建议使用以下代码行:

NewClass = types.new_class("NewClassName", (SuperClass,), kwds = { "namespace" : my_ontology })

在我的例子中,这等于

types.new_class("NewClassName", (onto["ParentClass"],), kwds={'namespace' : onto})

然而,当我运行上述代码时,我得到以下异常:

 Traceback (most recent call last):
    (onto[object.get('owl_dataProperty_parent')],), kwds={'namespace' : onto})
    File "/usr/lib/python3.6/types.py", line 62, in new_class
    return meta(name, bases, ns, **kwds)
 TypeError: __new__() got an unexpected keyword argument 'namespace

我不知道那里出了什么问题,经过几个小时的调试我仍然一无所知。 我正在使用 Python 3.6.6 和 owlready2

的 0.11 版

我发现根本不需要 "namespace" 属性。所以以下工作正常:

types.new_class("NewClassName", (onto["ParentClass"],))

尽管这不能解决整体问题,但它回答了我的问题。

另外,一般都是这样拼接的,参见owlready forum。所以也许是时候更新文档了。