Hi,
Yun schrieb am 27.04.22 um 06:42:
> I'm back with some more questions about pure python mode, this time around
> importing and referencing @cython.cclass-style cython modules.
>
> *General idea I'm trying to understand:*
> If I want to use @cython.cclass in .py modules, what's the best practice
> (or examples I can look at) to properly reference & call them in both the
> python interpreted and compiled cython modes?
>
> *1. Are conditional imports needed everywhere they are referenced?* ie,
> if cython.compiled:
> from cython.cimports.cython_decorated_module <
http://module1.py> import
> Foo
> else:
> from cython_decorated_module <
http://module1.py> import Foo
Not sure what exactly you are asking here. If you want to use them from
within the same module, then you do not need anything else. If you mean to
use them (specifically, their Cython-/C-level features) from another
module, then you need to declare them in a .pxd file, so that other modules
can cimport from it. That cimport can either happen in the module's own
.pxd file (if you want to add one) or via a "cython.cimports.*" import in a
.py module.
We should make sure that this is properly explained in the documentation
about sharing declarations across modules.
> *2. Is there a similar option for .py files like cimport_from_pyx*
> This option allows for compiling *.pyx without needing to define a
> corresponding *.pxd; is there a way to do this with @cython.cclass *.py
> files, or do I need to define a .pxd for them manually?
> (And just to confirm, I definitely need a .pxd file that corresponds to any
> @cython.cfunc that I define?)
There is no "cimport_from_py" option, currently. Might be worth adding,
given that we do have "cimport_from_pyx". It'd be a bit more costly at
build time, given that it not only has to parse an entire .pyx (or .py)
file but also apply all type annotations. Parsing .pxd files tends to be
quicker since they contain little (or no) actual implementation code. But
I'm sure there are people out there who would be happy to pay a bit of
build time overhead for the additional comfort of not having to write .pxd
files.
Stefan