[C++-sig] How to define a Python metaclass with Boost.Python?
paulc.mnt at gmail.com
Sat Feb 11 22:15:32 CET 2012
> This is indeed a perfectly valid way to make a metaclass, and it should
> definitely work to do the same thing in C++ using the C API and/or
> The trouble is using that metaclass as the type of a Boost.Python-wrapped
> C++ class...that's what I think is basically impossible.
> Of course, if you just want to inject some attributes into a wrapped
> class, you can do that in Boost.Python without a metaclass. You'd write a
> function that takes a boost::python::object and adds the attributes you
> want to it. Calling that function with a boost::python::class_ object
> would then add the attribute, just like a metaclass would.
I don't think it's impossible to set a metaclass of another class with
Boost.Python. The trick is actually pretty similar to the one above.
>>> class Foo:
>>> Foo = FooMeta(Foo.__name__, Foo.__bases__, dict(Foo.__dict__))
You first define the class in Boost.Python as you normally would, then do
the equivalent of the code above. This should work, unless, of course,
there's some subtle detail I'm not aware of.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Cplusplus-sig