我正在使用Python绑定(使用boost :: python)表示存储在文件中的数据的C++库.我的大多数半技术用户将使用Python与之交互,因此我需要尽可能将其设为Pythonic.但是,我也会让C++程序员使用API,所以我不想在C++方面妥协以适应Python绑定.
图书馆的很大一部分将由容器制成.为了让python用户看起来很直观,我希望它们的行为类似于python列表,即:
# an example compound class
class Foo:
def __init__( self, _val ):
self.val = _val
# add it to a list
foo = Foo(0.0)
vect = []
vect.append(foo)
# change the value of the *original* instance
foo.val = 666.0
# which also changes the instance inside the container
print vect[0].val # outputs 666.0
Run Code Online (Sandbox Code Playgroud)
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/register_ptr_to_python.hpp>
#include <boost/shared_ptr.hpp>
struct Foo {
double val;
Foo(double a) : val(a) {}
bool operator == (const …Run Code Online (Sandbox Code Playgroud)