在扩展模块中为用户定义的Python类增强Python到_Python_转换器 < P> >我想返回下面的Python类的实例,它模仿了我的C++扩展模块:/P>>三状态逻辑的Booo::TrBooL class Tribool(object): def __init__(self, value = None): if any(value is v for v in (False, True, None)): self.value = value else: raise ValueError("Tribool must be False, True or None") def __nonzero__(self): raise TypeError("Tribool may not be used as an implicit bool") def __eq__(self, other): if isinstance(other, Tribool): return self.value is other.value if any(other is v for v in (False, True, None)): return self.value is other raise TypeError("Tribool can only be compared to another Tribool or False, True, or None") def __ne__(self, other): return not (self == other) def __invert__(self): if self.value is False: return Tribool(True) elif self.value is True: return Tribool(False) elif self.value is None: return Tribool(None) raise ValueError("Tribool must be False, True or None") def __and__(self, other): if (self.value is False) or (other.value is False): return Tribool(False) if (self.value is None) or (other.value is None): return Tribool(None) return Tribool(True) def __or__(self, other): if (self.value is True) or (other.value is True): return Tribool(True) if (self.value is None) or (other.value is None): return Tribool(None) return Tribool(False) def __xor__(self, other): if (self.value is None) or (other.value is None): return Tribool(None) if self == other: return Tribool(False) return Tribool(True) def __str__(self): if any(self.value is v for v in (False, True, None)): return str(self.value) raise ValueError("Tribool must be False_, True_ or Maybe_") def __repr__(self): return "Tribool(%s)" % str(self)

在扩展模块中为用户定义的Python类增强Python到_Python_转换器 < P> >我想返回下面的Python类的实例,它模仿了我的C++扩展模块:/P>>三状态逻辑的Booo::TrBooL class Tribool(object): def __init__(self, value = None): if any(value is v for v in (False, True, None)): self.value = value else: raise ValueError("Tribool must be False, True or None") def __nonzero__(self): raise TypeError("Tribool may not be used as an implicit bool") def __eq__(self, other): if isinstance(other, Tribool): return self.value is other.value if any(other is v for v in (False, True, None)): return self.value is other raise TypeError("Tribool can only be compared to another Tribool or False, True, or None") def __ne__(self, other): return not (self == other) def __invert__(self): if self.value is False: return Tribool(True) elif self.value is True: return Tribool(False) elif self.value is None: return Tribool(None) raise ValueError("Tribool must be False, True or None") def __and__(self, other): if (self.value is False) or (other.value is False): return Tribool(False) if (self.value is None) or (other.value is None): return Tribool(None) return Tribool(True) def __or__(self, other): if (self.value is True) or (other.value is True): return Tribool(True) if (self.value is None) or (other.value is None): return Tribool(None) return Tribool(False) def __xor__(self, other): if (self.value is None) or (other.value is None): return Tribool(None) if self == other: return Tribool(False) return Tribool(True) def __str__(self): if any(self.value is v for v in (False, True, None)): return str(self.value) raise ValueError("Tribool must be False_, True_ or Maybe_") def __repr__(self): return "Tribool(%s)" % str(self),c++,python,boost,boost-python,C++,Python,Boost,Boost Python,我相信我需要编写一个to_python_转换器,但我不知道如何为用户定义的python类型编写转换器。我已经阅读了boostpython文档,并使用Google进行了搜索,但没有找到一个好的示例。感谢您的帮助 更新: 以下C++代码将将TRIPBOL转换为Python值TRUE、FALSE和NORE,这是部分解决方案: struct tribool_to_Tribool { static PyObject *convert(tribool const& value) {

我相信我需要编写一个to_python_转换器,但我不知道如何为用户定义的python类型编写转换器。我已经阅读了boostpython文档,并使用Google进行了搜索,但没有找到一个好的示例。感谢您的帮助


更新

以下C++代码将将TRIPBOL转换为Python值TRUE、FALSE和NORE,这是部分解决方案:

struct tribool_to_Tribool
{
    static PyObject *convert(tribool const& value)
    {
        if (value == true) return boost::python::incref(boost::python::object(true).ptr());
        else if (value == false) return boost::python::incref(boost::python::object(false).ptr());
        else return boost::python::incref(boost::python::object().ptr());
    }
};

to_python_converter<tribool,tribool_to_Tribool>();
struct tribool\u to\u tribool
{
静态PyObject*转换(tribool常量和值)
{
if(value==true)返回boost::python::incremf(boost::python::object(true).ptr());
else if(value==false)返回boost::python::incremf(boost::python::object(false).ptr());
否则返回boost::python::incremf(boost::python::object().ptr());
}
};
to_python_converter();

我无法理解的部分是如何返回纯Python Tribool类的实例,而不是从转换例程返回值True、False和None。

这可以通过与从Python方法返回
Tribool
相同的方式来实现。在这两种语言中,都需要获得
Tribool
Python类对象的句柄,然后调用它。返回的对象将是
Tribool
类型的实例

例如:

来自tribool进口tribool
x=Tribool(假)
相当于:

namespace python=boost::python;
python::object tribool_class=python::import(“tribool”).attr(“tribool”);
python::object x=tribool_类(false);

下面是一个简化的例子,其中一个用户定义的Python模块(<代码>垃圾邮件< /代码>)包含一个Python类(SPAM < /C> >),一个C++ Python扩展模块(示例)提供了一个<代码> MauxySPAM < /C>函数,创建了<代码>垃圾邮件>垃圾邮件对象。

spam.py
Python模块:

class Tribool(object):

def __init__(self, value = None):
    if any(value is v for v in (False, True, None)):
        self.value = value
    else:
        raise ValueError("Tribool must be False, True or None")

def __nonzero__(self):
    raise TypeError("Tribool may not be used as an implicit bool")

def __eq__(self, other):
    if isinstance(other, Tribool):
        return self.value is other.value
    if any(other is v for v in (False, True, None)):
        return self.value is other
    raise TypeError("Tribool can only be compared to another Tribool or False, True, or None")

def __ne__(self, other):
    return not (self == other)

def __invert__(self):
    if self.value is False:
        return Tribool(True)
    elif self.value is True:
        return Tribool(False)
    elif self.value is None:
        return Tribool(None)
    raise ValueError("Tribool must be False, True or None")

def __and__(self, other):
    if (self.value is False) or (other.value is False):
        return Tribool(False)
    if (self.value is None) or (other.value is None):
        return Tribool(None)
    return Tribool(True)

def __or__(self, other):
    if (self.value is True) or (other.value is True):
        return Tribool(True)
    if (self.value is None) or (other.value is None):
        return Tribool(None)
    return Tribool(False)

def __xor__(self, other):
    if (self.value is None) or (other.value is None):
        return Tribool(None)
    if self == other:
        return Tribool(False)
    return Tribool(True)

def __str__(self):
    if any(self.value is v for v in (False, True, None)):
        return str(self.value)
    raise ValueError("Tribool must be False_, True_ or Maybe_")

def __repr__(self):
    return "Tribool(%s)" % str(self)
类垃圾邮件(对象):
定义初始值(自身,值):
自我价值=价值
定义(自我):
返回self._repr__()+”的值为“+str(self.value)
example.cpp
扩展模块:

class Tribool(object):

def __init__(self, value = None):
    if any(value is v for v in (False, True, None)):
        self.value = value
    else:
        raise ValueError("Tribool must be False, True or None")

def __nonzero__(self):
    raise TypeError("Tribool may not be used as an implicit bool")

def __eq__(self, other):
    if isinstance(other, Tribool):
        return self.value is other.value
    if any(other is v for v in (False, True, None)):
        return self.value is other
    raise TypeError("Tribool can only be compared to another Tribool or False, True, or None")

def __ne__(self, other):
    return not (self == other)

def __invert__(self):
    if self.value is False:
        return Tribool(True)
    elif self.value is True:
        return Tribool(False)
    elif self.value is None:
        return Tribool(None)
    raise ValueError("Tribool must be False, True or None")

def __and__(self, other):
    if (self.value is False) or (other.value is False):
        return Tribool(False)
    if (self.value is None) or (other.value is None):
        return Tribool(None)
    return Tribool(True)

def __or__(self, other):
    if (self.value is True) or (other.value is True):
        return Tribool(True)
    if (self.value is None) or (other.value is None):
        return Tribool(None)
    return Tribool(False)

def __xor__(self, other):
    if (self.value is None) or (other.value is None):
        return Tribool(None)
    if self == other:
        return Tribool(False)
    return Tribool(True)

def __str__(self):
    if any(self.value is v for v in (False, True, None)):
        return str(self.value)
    raise ValueError("Tribool must be False_, True_ or Maybe_")

def __repr__(self):
    return "Tribool(%s)" % str(self)
#包括
boost::python::object make_spam(boost::python::object value)
{
//从垃圾邮件导入垃圾邮件
//返回垃圾邮件(值)
返回boost::python::import(“spam”).attr(“spam”)(值);
}
BOOST_PYTHON_模块(示例)
{
boost::python::def(“make_spam”、&make_spam);
}
交互式Python:

>>导入示例
>>>打印示例。生成垃圾邮件(错误)
值为False
>>>打印示例。生成垃圾邮件(True)
有一个真正的价值
>>>打印示例。生成垃圾邮件(无)
具有“无”的值

<代码>第二个代码片段是我丢失的一段信息——如何在C++端上获得纯Python类的句柄。现在一切正常。我假设
python::import(“tribool”)
表达式中的额外空间是多余的?@RandomBits很高兴它能为您工作。另外,对于额外的空间,我已经编辑掉了。对于三态逻辑,可能对您有用。