2009/6/4 J. Michael Owen <mikeo...@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
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to