By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,045 Members | 1,389 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,045 IT Pros & Developers. It's quick & easy.

swig & Python question

P: n/a
I am playing around with SWING building a Python module using the no brainer
example in http://www.swig.org/tutorial.html. With that first example,
/* File : example.c */

#include <time.h>
double My_variable = 3.0;

int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}

int my_mod(int x, int y) {
return (x%y);
}

char *get_time()
{
time_t ltime;
time(&ltime);
return ctime(&ltime);
}
and using the swig file of:
/* example.i */
%module example
%{
/* Put header files here (optional) */
%}

extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
I was able to execute this Python script:

import example
example.fact(5) 120 example.my_mod(7,3) 1 example.get_time() 'Sun Feb 11 23:01:07 1996'

However, when I try to access the gloabl variable My_variable by doing:

print example.cvar

I get a blank (rather then a value of 3.0).

Why?

--
It's me
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"It's me" <it***@yahoo.com> wrote in message
news:oD*******************@newssvr13.news.prodigy. com...
I am playing around with SWING building a Python module using the no brainer example in http://www.swig.org/tutorial.html. With that first example,


Oops! Soapy fingers. "SWIG" - not "SWING".

--
It's me.
Jul 18 '05 #2

P: n/a
It's me wrote:
"It's me" <it***@yahoo.com> wrote in message
news:oD*******************@newssvr13.news.prodigy. com...
I am playing around with SWING building a Python module using the no


brainer
example in http://www.swig.org/tutorial.html. With that first example,

Oops! Soapy fingers. "SWIG" - not "SWING".

--
It's me.


I have used SWIG before, and it's not always such a "no-brainer". In
fact, it rarely is except for trivial examples. But it can work. I think
it is best suited for wrapping large libraries. For small stuff, it
would be better to just do it "manually" using the Python C API.
Good luck.

--
It's not me.


--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart <kd***@kdart.com>
public key: ID: F3D288E4
================================================== ==========================
Jul 18 '05 #3

P: n/a
Whether SWIG will work in a "no brainer" way or not depends on the original
code, I think. If the original code uses a very convoluted design, of
course things will get hairy. If the package uses a very clean structure,
I think you will find SWIG works out very nicely.

The intriguing things is, however, once you structure the package to a form
SWIG would work, it opens up the door to support multiple script languages
(and they have a long list of supported script languages).

If you hand crafted it to run the Python-C API, then you can only use Python
as script.

--
It's me
"Keith Dart" <kd***@kdart.com> wrote in message
news:41**************@kdart.com...
It's me wrote:
"It's me" <it***@yahoo.com> wrote in message
news:oD*******************@newssvr13.news.prodigy. com...
I am playing around with SWING building a Python module using the no
brainer
example in http://www.swig.org/tutorial.html. With that first example,

Oops! Soapy fingers. "SWIG" - not "SWING".

--
It's me.


I have used SWIG before, and it's not always such a "no-brainer". In
fact, it rarely is except for trivial examples. But it can work. I think
it is best suited for wrapping large libraries. For small stuff, it
would be better to just do it "manually" using the Python C API.
Good luck.

--
It's not me.


--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo--------------------------------------

-- Keith Dart <kd***@kdart.com>
public key: ID: F3D288E4

================================================== ==========================
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.