Accessing Python variables in an extension module

Discussion in 'Python' started by MD, Jul 16, 2007.

  1. MD

    MD Guest

    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
    MD, Jul 16, 2007
    #1
    1. Advertising

  2. MD <> wrote:

    > 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
    Alex Martelli, Jul 16, 2007
    #2
    1. Advertising

  3. MD

    MD Guest

    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, (Alex Martelli) wrote:
    > MD <> wrote:
    > > 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
    MD, Jul 16, 2007
    #3
  4. 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
    Diez B. Roggisch, Jul 16, 2007
    #4
  5. 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)


    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
    Alex Martelli, Jul 16, 2007
    #5
  6. MD

    MD Guest

    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, (Alex Martelli) wrote:
    > 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)

    >
    > 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
    MD, Jul 16, 2007
    #6
  7. MD <> writes:

    > 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.
    Hrvoje Niksic, Jul 16, 2007
    #7
  8. MD <> wrote:

    > 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
    Alex Martelli, Jul 17, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. subrahmanya raju via .NET 247

    accessing session variables in a asp.net module

    subrahmanya raju via .NET 247, Jun 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,238
    Craig Deelsnyder
    Jun 26, 2004
  2. dwok
    Replies:
    7
    Views:
    685
    Hal Rosser
    Mar 4, 2005
  3. Ilariu Raducan
    Replies:
    2
    Views:
    340
    Ilariu Raducan
    Jul 14, 2004
  4. Replies:
    3
    Views:
    389
    Bruno Desthuilliers
    Sep 5, 2007
  5. Floris Bruynooghe
    Replies:
    1
    Views:
    330
    Floris Bruynooghe
    Dec 24, 2008
Loading...

Share This Page