I have read in this link: https://github.com/cython/cython/wiki/FAQ#id35 that my Cython 0.20.1 should be able to support const.
However my following code does not compile:
cdef const double a = 2.5
print(a)
During compilation it says
test.pyx:5:5: Assignment to const 'a'
Traceback (most recent call last):
File "setup1.py", line 11, in <module>
ext_modules = cythonize(extensions)
File "X:\WinPython3\python-3.3.5.amd64\lib\site-packages\Cython\Build\Dependencies.py", line 785, in cythonize
cythonize_one(*args[1:])
File "X:\WinPython3\python-3.3.5.amd64\lib\site-packages\Cython\Build\Dependencies.py", line 902, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: test.pyx
There are two kinds of function definition in Cython: Python functions are defined using the def statement, as in Python. They take Python objects as parameters and return Python objects. C functions are defined using the cdef statement in Cython syntax or with the @cfunc decorator.
Cython is a programming language that blends Python with the static type system of C and C++. cython is a compiler that translates Cython source code into efficient C or C++ source code. This source can then be compiled into a Python extension module or a standalone executable.
To make your Python into Cython, first you need to create a file with the . pyx extension rather than the . py extension. Inside this file, you can start by writing regular Python code (note that there are some limitations in the Python code accepted by Cython, as clarified in the Cython docs).
const
works for function arguments, but not for this construct because of how Cython compiles it to C.
cdef double a = 2.5
becomes
double __pyx_v_a;
/* lots of other declarations */
__pyx_v_a = 2.5;
and this wouldn't work with a const
variable. In effect, you cannot use const
in Cython in all the places were you can use it in C.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With