Commit cf88658e authored by Stefan Behnel's avatar Stefan Behnel

Make it clear in the documentation how initial assignments work for local and global variables.

See https://github.com/cython/cython/issues/4594#issuecomment-1022092245
parent 44b5b0d1
......@@ -77,6 +77,8 @@ define global C variables.
g: cython.int[42]
h: cython.p_float
i = j = 5
.. group-tab:: Cython
.. code-block:: cython
......@@ -87,7 +89,46 @@ define global C variables.
cdef int i, j, k
cdef float f, g[42], *h
Moreover, C :keyword:`struct`, :keyword:`union` and :keyword:`enum` are supported:
i = j = 5
As known from C, declared global variables are automatically initialised to
``0``, ``NULL`` or ``None``, depending on their type. However, also as known
from both Python and C, for a local variable, simply declaring it is not enough
to initialise it. If you use a local variable but did not assign a value, both
Cython and the C compiler will issue a warning "local variable ... referenced
before assignment". You need to assign a value at some point before first
using the variable, but you can also assign a value directly as part of
the declaration in most cases:
.. tabs::
.. group-tab:: Pure Python
.. code-block:: python
a_global_variable = declare(cython.int, 42)
def func():
i: cython.int = 10
f: cython.float = 2.5
g: cython.int[4] = [1, 2, 3, 4]
h: cython.p_float = cython.address(f)
.. group-tab:: Cython
.. code-block:: cython
cdef int a_global_variable
def func():
cdef int i = 10, j, k
cdef float f = 2.5
# cdef float g[4] = [1,2,3,4] # currently not supported
cdef float *g = [1, 2, 3, 4]
cdef float *h = &f
In addition to the basic types, C :keyword:`struct`, :keyword:`union` and :keyword:`enum`
are supported:
.. tabs::
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment