C always provide light-weight processing among all high-level programming language. Using python give advantages which we can extend into C as python modules. There are several workflow we should know before starting.
1. Naming Convention
Preferable we create C files always followed with module to indicating it’s a “Module”.
2. Using Python Dev API
We should have “python-dev” installed in our Linux or Ubuntu.
Then we can call Python API to wrap C codes as python modules.
3. Use setup.py
Because we will treat C extension as python modules, we should provide setup.py installation.
At this example, I will show how to write “temperature” module in C and extend into python as module.
First, we create temperaturemodule.c. Then we build design by :
#CREATE METHOD fahrenheit_to_celcius
#CREATE METHOD celcius_to_fahrenheit
#REGISTER ALL METHODS
1. #INCLUDE HEADER
We should call python header “Python.h”
2. #CREATE METHOD fahrenheit_to_celcius and #CREATE METHOD celcius_to_fahrenheit
We create to function to convert fahrenheit to celcius and vice-versa.
3. #REGISTER ALL METHODS
Then we should register all methods
The last thing, we should initialize this modules.
The codes :
Inheritance is a way to reuse code of existing object. Differ with Java, Python have more flexible inheritance. We can derived from base class and overide as many as we want to. So what most simple implementation of inheritance in Python?
Open your Python console interpreter and start typing: