Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2003-08-05 06:22:34

Christopher Dawson <dawson_at_[hidden]> writes:

> Hi there,
> I am wrapping a class with an operator()(int, int) which I'd like to
> make available in Python as a subscript operator, but haven't been able
> to find any documentation on this nor guess the appropriate .def
> Is this possible and if so can anyone help?

Please bring your Boost.Python questions to the C++-sig:

        .def("__call__", &myclass::operator())

should work if you want round-paren subscripts. That won't help you
if you need assignment, though.

   x(a, b) = 1

isn't legal Python.

If you need assignment, you need square subscripts:

   x[a,b] = 1

For that, write thin wrapper functions and expose them as __getitem__
and __setitem__. See the Python docs
( for more info.


Dave Abrahams
Boost Consulting

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at