Monday 14 July 2014

Python Binding - Introduction

In this post am gonna give a quick overview on what Python binding actually is.

Python binding is nothing but, exposing C/C++ classes, data members/functions to Python. What does 'exposing' mean? "Member functions and data members written in C/C++ can be accessed by(or will be available to) Python by this method".
You may ask, why C++ - Python interface? Parts of program that require more flexibility than speed/efficiency can be written in Python and critical portions can be coded in C++. 

According to PythonWiki, Python has more than one way to make C/C++ functionality accessible to Python.

I am choosing Boost.Python, mainly because of its stable support.

According to QuickStart, the Boost Python Library is a framework for interfacing Python and C++. It allows quick and seamless exposure of C++ classes,functions and objects to Python, and vice-versa using no special tools - just C++ compiler. It is designed to wrap C++ interfaces non-intrusively, so that the C++ code need not be modified/altered at all inorder to wrap it, making Boost.Python ideal for exposing 3rd part libraries to Python.
The library's use of advanced metaprogramming techniques simplifies its syntax for users, so that wrapping code takes on the look of a kind of declarative Interface Definition Language(IDL).

Advantages of Boost.Python
  • Comprehensive lifetime management facilities for low-level C++ pointers and references (Call Policies).
  • Support for C++ virtual functions that can be overridden in Python.
  • Wrapping of overloaded operators, STL container classes (like vector and map).
  • Support for organizing extensions as Python packages, with a central registry for inter-language type conversions.
  • As mentioned earlier, it allows exposing C++ classes and functions to Python, without an additional wrapping language, but simply using C++ compiler.

Compiling the library initially and getting it to work in your environment or IDE can be quite frustrating and time consuming.

In the next post I will guide you through the setup of Boost.Python that will ease your burden!

Happy learning!

No comments:

Post a Comment