TurbulentWinds
TurbulentWinds

Reputation: 11

Do the values of global variables in a C extension to Python persist on function calls?

Basically, say I have some global variable foo in my C extension, set to an initial value of 3 like so:

int foo = 3;

And say the value of foo is changed to 4 within function call foobar:

int foobar() {
    foo = 4;
    return 0;
}

If I make another call on the C extension, is the value of foo reset to 3 or does the value of foo persist across function calls and remain 4?

It seems to make sense that foo would persist but I figured better safe than sorry.

Upvotes: 0

Views: 47

Answers (1)

gelonida
gelonida

Reputation: 5630

A shared object or a dll is loaded once per python process (except you have explicit code closing reopening the C extension)

Therefore global vars and static vars will persist for one process. (and its threads)

Upvotes: 1

Related Questions