Accessing C global variables from Python

dj trombley badzen at yifan.net
Fri Dec 17 02:40:47 EST 1999


Curtis Jensen wrote:
> 
> Is there a way to access the global variables in a C class from Python?
> Without passing arguments back and forth between the two languages.
> Also, is it possible to do the same thing between C and Fortran?  If so,
> can some one explain it to me.  Thanks.
> 
> --
> Curtis Jensen
> cjensen at be-research.ucsd.edu
> FAX (425) 740-1451

The easy answer is "No." =)

C global variables are no more than locations in memory.
Therefore, to access them from Python, you'd have to access arbitrary
addresses 
in memory, which the Python interpreter probably does not have
permission to do.

Technically speaking, there are various shared memory schemes that one
can use to
allow "interoperation" between the two languages, but this approach is
sort of Wrong.

If the thing absolutely must be done, the Right solution is to write a
Python module in
C which will provide an object whose members access the various
variables who you wish to expose.

As for fortran, I don't know it.  But in general it will depend in the
latter approach on
if there is an interface or no for embedding C code.

-dj

Dave Trombley
<badzen at yifan.net>



More information about the Python-list mailing list