`SWIG/Examples/python/simple/`

## Simple Python Example

`$Header: /cvsroot/swig/SWIG/Examples/ocaml/simple/index.html,v 1.4 2004/11/02 22:13:36 wsfulton Exp $`

This example illustrates how you can hook Python to a very simple C program containing a function and a global variable.

## The C Code

Suppose you have the following C code:

>     /* File : example.c */

>     /* A global variable */
>     double Foo = 3.0;

>     /* Compute the greatest common divisor of positive integers */
>     int gcd(int x, int y) {
>       int g;
>       g = y;
>       while (x > 0) {
>         g = x;
>         x = y % x;
>         y = g;
>       }
>       return g;
>     }

## The SWIG interface

Here is a simple SWIG interface file:

>     /* File: example.i */
>     %module example

>     extern int gcd(int x, int y);
>     extern double Foo;

## Compilation

1. `swig -python example.i`

2. Compile `example_wrap.c` and `example.c` to create the extension `examplemodule.so`.

## Using the extension

Click here to see a script that calls our C functions from Python.

## Key points

- Use the `import` statement to load your extension module from Python. For example:

  >     import example

- C functions work just like Python functions. For example:

  >     g = example.gcd(42,105)

- C global variables are accessed through a special variable called 'cvar'. For example:

  >     a = example.cvar.Foo