No, there's not a way to declare a set of automatic conversions.
Cython is not a wrapper-generator. You can write utility functions to
do the conversions yourself and call them manually (being careful, of
course, to get the memory allocation semantics of these more
complicated structs correct). This also affords the opportunity to
give your wrapping a more Pythonic interface; often a 1:1 mapping
between C++ and Python is not the best API.
- Robert
pyx:MyCustomDict()MyCustomDict(string, CustomValue)
cdef cMyCustomDict *thisptrdef __cinit__(self, *args):
if args and isinstance(args[0], PyObject):
#if args is a python type convert to my CustomValue type somehowself.thisptr = new MyCustomDict(convert_to_customvaltype(args[0]))
else:
self.thisptr = new MyCustomDict()
It is not possible to register custom conversions to be used on
casts; only a fixed number of basic conversions (essentially numeric
values and char*) are supported. The way you've written it, calling
convert_to_customvaltype (that you wrote) explicitly, is correct.
(Note that args[0] is always going to be a python object if it's
there, unless by PyObject you mean something else.)
- Robert
But would be nice to have, right?
--
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
3000 Santa Fe, Argentina
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169
The Zen of Python: explicit is better than implicit.
But, yes, if this can be done right, it could be nice.
- Robert
+1
> But, yes, if this can be done right, it could be nice.
-0
It's not only "when done right" on the compiler side but also depends a lot
on the user side. This could get us too close to C++ code.
Stefan
The problem with C++ is that things are many times implicit. For
example, you have to use explicitly use "explicit" in constructors to
avoid implicit conversions. But if the user EXPLICITLY ask Cython to
use a custom to/from C<->Python converters, I do not think that's too
bad.
For example, I have a little use case from my own code: In petsc4py, I
wrap PETSc. PETSc has a numeric data type "PetscScalar" typedefed to
"double" or "double complex" depending on how you built PETSc. In
Cython, I cannot declare a type that could be either "double" or
"double complex" at C compile time. Then I have to EXPLICITLY call a
toScalar()/asScalar() almost everywhere. That's really annoying, I
would really like to say: "PetscScalar" is number-like, use
toScalar()/asScalar() for converting to C/from Python.
Yes.
> For
> example, you have to use explicitly use "explicit" in constructors to
> avoid implicit conversions. But if the user EXPLICITLY ask Cython to
> use a custom to/from C<->Python converters, I do not think that's too
> bad.
It's implicit where it's used. It's unclear what the scope of such a
declaration would be.
> For example, I have a little use case from my own code: In petsc4py, I
> wrap PETSc. PETSc has a numeric data type "PetscScalar" typedefed to
> "double" or "double complex" depending on how you built PETSc. In
> Cython, I cannot declare a type that could be either "double" or
> "double complex" at C compile time. Then I have to EXPLICITLY call a
> toScalar()/asScalar() almost everywhere. That's really annoying, I
> would really like to say: "PetscScalar" is number-like, use
> toScalar()/asScalar() for converting to C/from Python.
The real vs. complex decided at C compile time seems like an even
thornier issue.
- Robert