Cython to Wrap Existing C Code

What is Cython ? 

It is an optimizing static compiler for both the Python programming language and the extended Cython programming language. It is used to make it easy to write C extensions for Python as easy as Python itself.

It comes up with many helpful features :

  • Writing a Python code that calls back and forth from and to C/C++ code.
  • Easily tuning of readable Python code into plain C performance by adding static type declarations.
  • Use of combined source code level debugging to find bugs in given Python, Cython and C code.
  • Efficient interaction with large data sets, e.g. using multi-dimensional NumPy arrays.
  • Integration with existing code and data from low-level or high-performance libraries and applications.

To make an extension with Cython is a tricky task to perform. Doing so, one needs to create a collection of wrapper functions. Assuming that the work code shown has been compiled into a C library called libwork. The code below will create a file named csample.pxd.

Code #1 :

# cwork.pxd 

# Declarations of "external" C 

# functions and structures 


cdef extern from "work.h": 


int gcd(int, int) 

int divide(int, int, int *) 

double avg(double *, int) nogil 

ctypedef struct Point: 

double x 

double y 

double distance(Point *, Point *) 

In Cython, the code above will work as a C header file. The initial declaration cdef externfrom "work.h" declares the required C header file. Declarations that follow are taken from the header. The name of this file is cwork.pxd. Next target is to create a work.pyx file which will define wrappers that bridge the Python interpreter to the underlying C code declared in the cwork.pxd file.


要查看或添加评论,请登录

ganesh kavhar的更多文章

  • Python NumPy for Data Science

    Python NumPy for Data Science

    NumPy Introduction NumPy stands for ‘Numerical Python.’ It is a package in Python to work with arrays.

  • Becoming a Better Programmer

    Becoming a Better Programmer

    A smart programmer is one who understands that his or her work is never truly done. It doesn't matter how much you…

  • Data Processing in Machine Learning

    Data Processing in Machine Learning

    ML | Understanding Data Processing Data Processing is a task of converting data from a given form to a much more usable…

  • Operator in C programing by ganesh kavhar

    Operator in C programing by ganesh kavhar

    An operator in a programming language is a symbol that tells the compiler or interpreter to perform a specific…

  • Numpy by ganesh kavhar

    Numpy by ganesh kavhar

    Python Numpy Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array…

  • Python for Data Analysis by ganesh kavhar

    Python for Data Analysis by ganesh kavhar

    A friend recently asked this and I thought it might benefit others if published here. This is for someone new to Python…

  • Data Classes in Python | An Introduction by ganesh kavhar

    Data Classes in Python | An Introduction by ganesh kavhar

    dataclass module is introduced in Python 3.7 as a utility tool to make structured classes specially for storing data.

  • Why learning C Programming is a must?

    Why learning C Programming is a must?

    C is a procedural programming language. It was initially developed by Dennis Ritchie between 1969 and 1973.

  • String Operation in Python

    String Operation in Python

    String: Strings in an array of bytes which represent Unicode characters in python. Python does not support character…

  • Data Types In Python

    Data Types In Python

    Today, in this article, we will learn about Python data types and their usage. This is a very important topic because…

社区洞察

其他会员也浏览了