[C++-sig] How to define a Python metaclass with Boost.Python?

Paul-Cristian Manta 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
> Boost.Python.
>
> 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.
>
>
> Jim
>
>
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:
...    pass
...
>>> 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...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120211/789f7d36/attachment.html>


More information about the Cplusplus-sig mailing list