[PyQt] [SIP] How to reasonably implement writable global variables?

Casper Ti. Vector caspervector at gmail.com
Tue Jul 2 17:28:59 BST 2013


Hello list, I am a newcomer to SIP (only one day experience), and please
tell me if I make a mistake.  Thanks :)

I found it quite hard to make read-write interface for (module-wide)
global variables (other than explicitly writing C/C++ functions to get and
set them).  For example:

test.sip:
> %Module test
> %ModuleHeaderCode
> #include "test.h"
> %End
> 
> int test;
> int get_test(void);
> void set_test(int x);

test.h:
> extern int test;
> int get_test(void);
> void set_test(int x);

test.cpp:
> int test = 0;
> int get_test(void) {
>   return test;
> }
> void get_test(int x) {
>   test = x;
> }

test.py:
> import test
> print(test.test) # 0
> print(test.get_test()) # 0
> test.set_test(1)
> print(test.test) # 0
> print(test.get_test()) # 1
> test.test = 2
> print(test.test) # 2
> print(test.get_test()) # 1

Thus `test' in python seems to be totally independently from its C++
counterpart once it is formed.  However, if wrapped in a class, they
would be bound together, and changes in one will affect the other.

So is there any elegant way to make global variables writable using
the `var = val' syntax?  Any suggestions are welcome :)

P.S. I know python is object-oriented, but I think this particular
     approach is still pratically useful in some situations (and OOP
     itself is not a "silver bullet"), so it might not be meaningless to
     implement this.

-- 
My current OpenPGP key:
4096R/0xE18262B5D9BF213A (expires: 2017.1.1)
D69C 1828 2BF2 755D C383 D7B2 E182 62B5 D9BF 213A



More information about the PyQt mailing list