[C++-sig] [] subscript notation with boost::python

Murray Cumming murrayc at murrayc.com
Mon Feb 23 12:39:19 CET 2009

I'm trying to support this notation in python:
something = record["somefieldname"]

I had that working with the Python C API, by implementing the len and
getitem functions, setting them in the PyTypeObject::tp_as_mapping
struct field.

I'm now trying to do this with boost::python, like so, because googling
has suggested that this should work:

  .def("__getitem__", &Glom::PyGlomRecord::getitem)
  .def("__len__", &Glom::PyGlomRecord::len)

But when I try to use that in Python, I get this error:
"Boost.Python.class' object is unsubscriptable"

Is there something else that I need to do?
Murray Cumming
murrayc at murrayc.com

More information about the Cplusplus-sig mailing list