It is an extentionmodule used to quickly generate Python extensions) the syntax of the tool is the mixed-race of the python language syntax and the c language syntax. it is easier to compile python extension modules than swig. maybe you will say that swig can directly use the c header file I. What is Cython??
It is a tool used to quickly generate # wiki/1514.html "target =" _ blank "> Python extention module
Its syntax is a mix of python and C.
It is easier to compile python extension modules than swig
You may say that swig can directly generate an extension module through the c header file, but swig does not support the callback function very well,
In addition, if swig is used, in many cases, you need to write additional code to convert input parameters to python objects and convert output to python objects, for example, if the input and output parameters of a encapsulated C function are input and output, and if the C function parameters contain callback functions
The types in Cython and C, such as int, float, long, and char *, are automatically converted to python objects when necessary, or from python objects to C types, an exception is thrown when the conversion fails, which is the most amazing part of Cython.
In addition, Cython supports callback functions well.
In short, if you need to write python extension modules, Cython is really a good tool.
II. cython
Install cython in linux:
1. install the source code package:
[blueelwang@pythontab ~]$ wget https://pypi.python.org/packages/b7/67/7e2a817f9e9c773ee3995c1e15204f5d01c8da71882016cac10342ef031b/Cython-0.25.2.tar.gz[blueelwang@pythontab ~]$ tar xzvf Cython-0.25.2.tar.gz[blueelwang@pythontab ~]$ cd Cython-0.25.2[blueelwang@pythontab ~]$ python setup.py install
2. pip package installation
[blueelwang@pythontab ~]$ sudo pip install Cython --install-option="--no-cython-compile"
3. install in Ubuntu
[blueelwang@pythontab ~]$ sudo apt-get install cython
After installation, enter cython to verify whether the installation is successful.
III. use 1. Compile the cython program with the extension. pyx, hello. pyx
def say_hello_to(name): print("Hello %s!" % name)
2. Compile the python program setup. py.
The purpose is to convert the hello. pyx program into hello. c and compile it into a so file.
from distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extext_modules = [Extension("hello", ["hello.pyx"])]setup( name = 'Hello world app', cmdclass = {'build_ext': build_ext}, ext_modules = ext_modules)
3. execute the python program
[blueelwang@pythontab ~]$ python setup.py build_ext --inplace
The execution result will generate two files: hello. c and hello. so (files encapsulated with PyObject)
4. use python to call hello. so. The Call file is test. py.
import hellohello.say_hello_to("hi,cython!!")
The main purpose of cython is to simplify the tedious encapsulation process of python calling C language programs and improve the speed of python code execution (the speed of c language execution is faster than that of python)
The above is a detailed introduction to Cython installation and usage experience. For more information, see other related articles in the first PHP community!