Cython

Материал из Xgu.ru

Перейти к: навигация, поиск


Cython -- это Python-подобный язык программирования с дополнительным синтаксическими конструкциями, предназначенными для статического объявления типов.

Код программы транслируется в C/C++ и компилируется как модуль расширения Python.

Cython может компилировать практически весь (но пока не весь) код Python.

Пример кода (со статическими типами):

def f(double x):
    return x**2-x

def integrate_f(double a, double b, int N):
    cdef int i
    cdef double s, dx
    s = 0
    dx = (b-a)/N
    for i in range(N):
        s += f(a+i*dx)
    return s * dx

Потом:

$ cython integrate.pyx
$ gcc -c -fPIC -I/usr/include/python2.7/ integrate.c
$ gcc -shared integrate.o -o integrate.so

Теперь можно использовать библиотеку integrate.so:

import integrate
print integrate.integrate_f(10,20,1000)

[править] Дополнительная информация

Источник — «http://xgu.ru/wiki/Cython»
This is a cached copy of the requested page, and may not be up to date.

Sorry! This site is experiencing technical difficulties.
Try waiting a few minutes and reloading.

(Can't contact the database server: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111) (localhost))


You can try searching via Google in the meantime.
Note that their indexes of our content may be out of date.