471,337 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Accessing Python variables in an extension module

MD
Hi,

I would like to access "variables" defined in my Python program in
a C module extension for Python. Is this possible? I looked at the
Python C API reference but didn't find anything there that could help
me.
Thanks in advance for any help/tips.

Regards,
-MD

Jul 16 '07 #1
7 1437
MD <ma****@gmail.comwrote:
Hi,

I would like to access "variables" defined in my Python program in
a C module extension for Python. Is this possible? I looked at the
Python C API reference but didn't find anything there that could help
me.
If they're global variables of a certain module XYZ, your C code may
"import" XZY (or look it up in the C API equivalent of the sys.modules
dict) and get those variables as attributes of object XYZ. Is that what
you mean by ``variables defined in your Python program''...?
Alex
Jul 16 '07 #2
MD
Hi Alex,
Thanks for your reply. It was exactly what I was looking for. Two
additional questions
1) Is there anyway to find out which modules a variable belongs to
when I have only its name (and its not qualified with the complete
name like module.varname)
2) Is there anyway to find the type of the object in C using something
like a switch statement? I was looking for something like this
switch type(object) {
STRING: "This is a string object";
break;
INTEGER: "This is an integer object";
break;
BOOLEAN: "This is a boolean object";
.........
.........
}
I don't want to run all the C Py***_Check functions on the object.
Something like the switch statement above will lead to nice and clean
code.
Thanks again for your reply.

Regards,
-Manas

On Jul 15, 11:02 pm, al...@mac.com (Alex Martelli) wrote:
MD <man...@gmail.comwrote:
Hi,
I would like to access "variables" defined in my Python program in
a C module extension for Python. Is this possible? I looked at the
Python C API reference but didn't find anything there that could help
me.

If they're global variables of a certain module XYZ, your C code may
"import" XZY (or look it up in the C API equivalent of the sys.modules
dict) and get those variables as attributes of object XYZ. Is that what
you mean by ``variables defined in your Python program''...?

Alex

Jul 16 '07 #3
MD wrote:
Hi Alex,
Thanks for your reply. It was exactly what I was looking for. Two
additional questions
1) Is there anyway to find out which modules a variable belongs to
when I have only its name (and its not qualified with the complete
name like module.varname)
No.
2) Is there anyway to find the type of the object in C using something
like a switch statement? I was looking for something like this
switch type(object) {
STRING: "This is a string object";
break;
INTEGER: "This is an integer object";
break;
BOOLEAN: "This is a boolean object";
.........
.........
}
I don't want to run all the C Py***_Check functions on the object.
Something like the switch statement above will lead to nice and clean
code.
In python,

obj.__class__.__name__

should work. I don't know what to write in C to accomplish that, but it
can't be too hard. Factor it away in a function.

Diez
Jul 16 '07 #4
MD <ma****@gmail.comwrote:
Hi Alex,
Thanks for your reply. It was exactly what I was looking for. Two
additional questions
1) Is there anyway to find out which modules a variable belongs to
when I have only its name (and its not qualified with the complete
name like module.varname)
Loop through all modules in sys.modules and you will find a set (which
may be empty) of modules which happen to have that name as an attribute.
It's a very peculiar thing to do (not clear what you expect to do based
on that information) but not difficult.
2) Is there anyway to find the type of the object in C using something
like a switch statement? I was looking for something like this
switch type(object) {
STRING: "This is a string object";
break;
INTEGER: "This is an integer object";
break;
BOOLEAN: "This is a boolean object";
.........
.........
}
I don't want to run all the C Py***_Check functions on the object.
Something like the switch statement above will lead to nice and clean
code.
Each Python object, in C, carries a pointer to its type object. Of
course there are unbounded numbers of types, but at least you can get
such information as the type's name and the module if any in which it
may have been defined, essentially like you'd do with type(somobj) if
you were working directly in Python.
Alex
Jul 16 '07 #5
MD
Hi Alex,
Thanks for the answer. Are there any C defines (for e.g. STRING,
BOOLEAN) corresponding to each Python type?

Thanks,
-Manas

On Jul 16, 9:53 am, al...@mac.com (Alex Martelli) wrote:
MD <man...@gmail.comwrote:
Hi Alex,
Thanks for your reply. It was exactly what I was looking for. Two
additional questions
1) Is there anyway to find out which modules a variable belongs to
when I have only its name (and its not qualified with the complete
name like module.varname)

Loop through all modules in sys.modules and you will find a set (which
may be empty) of modules which happen to have that name as an attribute.
It's a very peculiar thing to do (not clear what you expect to do based
on that information) but not difficult.
2) Is there anyway to find the type of the object in C using something
like a switch statement? I was looking for something like this
switch type(object) {
STRING: "This is a string object";
break;
INTEGER: "This is an integer object";
break;
BOOLEAN: "This is a boolean object";
.........
.........
}
I don't want to run all the C Py***_Check functions on the object.
Something like the switch statement above will lead to nice and clean
code.

Each Python object, in C, carries a pointer to its type object. Of
course there are unbounded numbers of types, but at least you can get
such information as the type's name and the module if any in which it
may have been defined, essentially like you'd do with type(somobj) if
you were working directly in Python.

Alex

Jul 16 '07 #6
MD <ma****@gmail.comwrites:
2) Is there anyway to find the type of the object in C using something
like a switch statement? I was looking for something like this
switch type(object) {
STRING: "This is a string object";
break;
INTEGER: "This is an integer object";
break;
BOOLEAN: "This is a boolean object";
.........
.........
}
Not switch, but the closest you'll get is:

if (object->ob_type == PyString_Type) {
... string
}
else if (object->ob_type == PyInt_Type) {
... int
}
else if (object->ob_type == PyBool_Type) {
... bool
}
I don't want to run all the C Py***_Check functions on the object.
Py*_Check are not expensive if the object really is of the target
type. They are necessary to support subtyping correctly.
Jul 16 '07 #7
MD <ma****@gmail.comwrote:
Hi Alex,
Thanks for the answer. Are there any C defines (for e.g. STRING,
BOOLEAN) corresponding to each Python type?
No, I know of no such "defines" -- what good would they do?
Alex
Jul 17 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

54 posts views Thread by Brandon J. Van Every | last post: by
113 posts views Thread by John Nagle | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.