将我自己的 get/set C++ 成员函数视为 SWIG 中的成员变量?

Treat my own get/set C++ member functions as a member variable in SWIG?

在其最低级别,SWIG 通过生成一对访问器函数来处理 C++ class member variables,例如来自:

class List {
public:
    int length;
    ...
};

SWIG 创建:

int List_length_get(List *obj) { return obj->length; }
int List_length_set(List *obj, int value) {
    obj->length = value;
    return value;
}

我可以提供自己的访问器,并告诉 SWIG 使用它们在 Python 中创建一个 "virtual" 成员变量吗?例如。来自:

class List {
public:
    int setLength(int aLength) { _length = aLength; return _length; }
    int getLength() { return _length; }
    ...
private:
    int _length;
    ...
};

告诉 SWIG 让我这样做:

>>> l = List();
>>> print(l.length)

在 Python?

希望我没有侵犯任何版权。

来自http://swig.10945.n7.nabble.com/attribute-directive-and-C-templates-td11288.html

I recently discovered how to use attribute.i and the %attribute directive to create attributes with customized behavior, as in the following (superfluous) example:

%attribute(Point, int, x, getX, setX); 
%attribute(Point, int, y, getY, setY); 
class Point { 
public: 
    int getX() { return _x }; 
    void setX(int x) { _x = x }; 
    int getY() { return _y }; 
    void setY(int y) { _y = y }; 
private: 
   int _x; 
   int _y; 
}