[C++-sig] class empty: pass

David Abrahams david.abrahams at rcn.com
Sat Jan 19 17:03:11 CET 2002


----- Original Message -----
From: "Ralf W. Grosse-Kunstleve" <rwgk at cci.lbl.gov>


> It frequently happens that I want to expose a simple C++ struct
> to Python, e.g.:
>
>   struct peak {
>     boost::array<int, 3> index;
>     double value;
>   };
>
> I often hesitate to use class_builder<> for small types
> like this because I assume that there is a large overhead
> associated with the proliferation of fully-fledged Python
> types. Therefore I just turn these structs into tuples
> or dictionaries. This is not very user-friendly, because
> it is not intuitive what peak[0] and peak[1] are, and
> the notation peak["index"] or peak["value"] is quite
> clunky.
>
> In pure Python I often use a construct like this:
>
> class empty: pass
>
> def foo():
>   result = empty()
>   result.index = (1,2,3)
>   result.value = 345
>   return result
>
> Could a similar mechanism be provided at the C++ level?
> E.g.:
>
>   py_peak = empty_builder<peak>;
>   py_peak.def(&peak::index, "index");
>   py_peak.def(&peak::value, "value");
>
> empty_builder<T> would not build a new extension class for each template
> argument, but reuse the same Python type.

Yes! But how would you make one? Remember, the type of the object is what
you call to create it:
    x = MyClass()

If you have a C++ function which can return them, though, you don't need a
separate type.

> Does this make sense? Would there be a significant saving
> in object code size (particularly in the new system that is
> based on the native Python type system)?

I think it would be small at best. Aren't you just doing this for
convenience? Why don't we discuss the ideal syntax, instead of worrying
about implementation details at this stage?

-Dave








More information about the Cplusplus-sig mailing list