[C++-sig] __getitem__ and __setitem__ for a Matrix class

David Abrahams dave at boost-consulting.com
Tue Dec 10 04:54:04 CET 2002


sashan <sashang at ihug.co.nz> writes:

> Hi
>
> I want to expose a matrix class's operator[]. To expose it as an
> rvalue I did the following
>
> boost::python::list GetList(Matrix3& self, int i)
> {
>     if (!(i < 3 && i > -1))
>     {
>         PyErr_SetObject(PyExc_IndexError, PyInt_FromLong(i));
>         boost::python::throw_error_already_set();
>     }
>     boost::python::list list;
>     list.append(self[i][0]);
>     list.append(self[i][1]);
>     list.append(self[i][2]);
>     return list;
> }
> .
> .
> .
> classMatrix3.def("__getitem__", &GetList);
>
> However I don't know/can't figure out how to expose it as an lvalue?

You can't; there are no lvalues in Python. That's why there's a
separate __setitem__ method.

> P: Egad! You astound me, Brain!
> B: That's a simple task, Pinky.

Noit!

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list