[PyQt] [SIP] How to reasonably implement writable global variables?
Phil Thompson
phil at riverbankcomputing.com
Wed Jul 3 09:30:29 BST 2013
On Wed, 3 Jul 2013 00:28:59 +0800, "Casper Ti. Vector"
<caspervector at gmail.com> wrote:
> 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.
You can't do it. Python needs to support module level descriptors for it
to be implemented.
Phil
More information about the PyQt
mailing list