我应该将什么数据传递给这个构造函数?

What data should I pass to this constructor?

我正在尝试使用函数 init 创建一个新实体。 但我不知道我应该给什么“e”。 “e”必须是一个元组。但是我怎么知道它长什么样子呢?

class entity_instance(object):
    def __init__(self, e):
        if isinstance(e, tuple):
            e = ifcopenshell_wrapper.new_IfcBaseClass(*e)
        super(entity_instance, self).__setattr__('wrapped_data', e)

这里是函数 new_IfcBaseClass:

def new_IfcBaseClass(schema_identifier, name):
"""new_IfcBaseClass(std::string const & schema_identifier, std::string const & name) -> entity_instance"""
    return _ifcopenshell_wrapper.new_IfcBaseClass(schema_identifier, name)

这是我的主要代码:

import ifcopenshell
from ifcopenshell import entity_instance
ifc=ifcopenshell.open('file.ifc')
Object = entity_instance()
entity_instance.__init__(('#1','ifctoken'))

我给了一个随机元组来测试,我是否可以构建一个新的entify_instance。但我得到“init() missing 1 required positional argument: 'e'”作为错误消息。

您不应该直接调用 __init__。这是一个 'dunder' (指的是两边的双下划线)或 'magical' (指的是他们添加的行为只是 'magically' 起作用而实际上没有调用它)方法。

__init__ 在实例化对象时调用,因此您的代码为:

import ifcopenshell
from ifcopenshell import entity_instance
ifc=ifcopenshell.open('file.ifc')
Object = entity_instance(('#1','ifctoken'))

我没有实际测试这个,因为我没有 .ifc 文件(或安装了 ifcopenshell),但由于你的问题更像是一个通用的 Python 问题,我相当确定这是在你的问题的至少一部分。

请注意,我没有更改命名,但 Object 将是一个非常糟糕的变量选择 - 大写 'O' 表明它实际上是 class (当它是一个实例)并且 'object' 几乎是通用的 - 即使这样 object 也是一个非常糟糕的选择,因为它隐藏了 Python 中的 object 超类型。