[C++-sig] class empty: pass

Ralf W. Grosse-Kunstleve rwgk at cci.lbl.gov
Sat Jan 19 11:13:56 CET 2002


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.
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)?

Thanks,
        Ralf




More information about the Cplusplus-sig mailing list