Global variables in a C extension for Python

Discussion in 'Python' started by Lorenzo Di Gregorio, Dec 28, 2011.

  1. Hello,

    I've written a C extension for Python which works so far, but now I've
    stumbled onto a simple problem for which I just can't find any example
    on the web, so here I am crying for help ;-)

    I'll trying to reduce the problem to a minimal example. Let's say I
    need to call from Python functions of a C program like:

    static int counter = 0;
    void do_something(...) {
    .... counter++; ...
    }
    void do_something_else(...) {
    .... counter++; ...
    }

    So they access a common global variable. I've written the wrappers
    for the functions, but I'd like to place "counter" in the module's
    space and have wrappers accessing it like self->counter. I do not
    need to make "counter" visible to Python, I just need the global
    static variable available for C.

    I've got somehow a clue of how this should work, but not much more
    than a clue, and I'd appreciate to see a simple example.

    Best Regards,
    Lorenzo
    Lorenzo Di Gregorio, Dec 28, 2011
    #1
    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. Wayne
    Replies:
    2
    Views:
    456
    Wayne
    Nov 11, 2003
  2. jubelbrus
    Replies:
    5
    Views:
    602
    JohnQ
    Jul 20, 2007
  3. mark4asp
    Replies:
    1
    Views:
    205
  4. Tony Archer

    Global Variables? Multi-Application Variables?

    Tony Archer, Nov 24, 2003, in forum: ASP General
    Replies:
    5
    Views:
    210
    Tony Archer
    Nov 25, 2003
  5. king
    Replies:
    2
    Views:
    158
    Tad McClellan
    Jun 27, 2006
Loading...

Share This Page