Skip to content

Permit to inject Python methods on the fly

This MR defines two new features.

  1. The ability to have different names between C++ member functions and their associated Python methods (py_names)
  2. The ability to inject Python methods on the fly without having a direct C++ member function to which they are associated (new_methods).

Rationale

  • Officially all symbols starting with two underscores are reserved for compiler and standard library implementers. This means we should not define function named __repr__, __format__... in C++ classes.
  • We don't necessarily want to define every method as C++ member functions as this doesn't always make sense, or because we need a small translation barrier.

Example

    classes:
        Vector3D:
            customization:
                py_names:
                    py_format: __format__
                new_methods:
                    __repr__:
                        doc:
                            Any documentation we wish.

                            It could be on several lines.
                        # List of extra includes required for implementing the lambda function
                        includes: ['"path/to/Vector3DFormat.hpp"']
                        # Exact text to inject
                        code: '+[](Vector3D const& v) { return FMTNS::format("{::, :Vector(:)}", v);}'

Note

This is a quick workaround. I'm quite sure better names for the new customization options could be used

Edited by Luc Hermitte

Merge request reports