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:
http://www.boost.org/more/mailing_lists.htm/#pysig.

        .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
(http://www.python.org/doc/ref/sequence-types.html) for more info.

HTH,

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net