473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 1517
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
5
by: daniel | last post by:
there's a dll extension used to be imported with no error under version 2.4.3, but the new python complains that the name of the module can't be found. seems not mentioned in the official...
113
by: John Nagle | last post by:
The major complaint I have about Python is that the packages which connect it to other software components all seem to have serious problems. As long as you don't need to talk to anything outside...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.