How do I expose constexpr for Cython?

The file Globals.h

contains the following constant definition:

namespace MyNameSpace {

/** Constants **/
constexpr index none = std::numeric_limits<index>::max();

}

      

... where index

is the typedef for uint64_t

.

How can I expose it in Cython and Python?

Unable to try:

cdef extern from "../cpp/Globals.h" namespace "MyNamespace":
    cdef index _none "MyNamespace::none"

none = _none

      

+3


source to share


1 answer


The syntax for exposing (global) constants is similar to the syntax for expanding simple attributes and for posting static elements , in your example the syntax is almost right, except you need to omit the statement cdef

, the statement cdef

is only for declaring new variables in Cython, not for adding information about external declared variables.

cdef extern from "../cpp/Globals.h" namespace "MyNamespace":
    index _none "MyNamespace::none"

none = _none

      

Then you can use none

in Python, if your Cython module has a name mymodule

, the import statement can be



from mymodule import none

      

if you want to make it none

available as a global name in your Python code.

+3


source







All Articles