[C++-sig] pybindgen: Why do wrapped C++ containers not look more like python containers?

Gustavo Carneiro gjcarneiro at gmail.com
Thu Jun 4 21:57:01 CEST 2009


2009/6/4 J. Michael Owen <mikeowen at llnl.gov>

> I've been looking at pybindgen and have a simple question -- why do the
> container wrappers not implement more of the python container interface?
>  For instance, you can expose the std::vector<int> like so:
>
> vecint = mod.add_container("std::vector<int>",         "int",    "vector",
> custom_name="vector_of_int")
>
> The resulting type won't let you take the length with "len" or index into
> the instances with [].  I would naively think maybe I could add these
> methods as appropriate to types using "add_method" like the following:
>
> vecint.add_method("size", "int", [], custom_name = "__len__")
>
> but because "Container" does not inherit from "CppClass" I don't see how to
> easily extend the container in this manner.  Is there a deep reason these
> things are this way?


No deep reason, just lazyness.  PyBindGen needs to implement some extra
slots [1].

As a workaround for now, just convert the vector to a python list firtst,
i.e. list(vector_object).  Then you can call len() on the list instead.  Not
pretty but effective.

[1] http://docs.python.org/c-api/typeobj.html#tp_as_sequence


-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20090604/04839960/attachment.htm>


More information about the Cplusplus-sig mailing list