[PyKDE] static const member variables

Nigel Stewart ns at fluent.com
Tue Jul 11 23:15:45 BST 2006


Hello all,

I'm sure this issue has come up before, in one way or another.

Our issue is to do with static const class member variables
such as:

class A
{
public:
     static const A zero;
     static const A one;
...
}

...In much the same manner as Qt provides QString::null.

The problem we're having is that without a concept of constness
in Python, we have the risk of python-code changing A::zero,
A::one or QString::null.

So, we were thinking of using %AccessCode (or something similar)
to provide copies to Python that can be changed, leaving the
C++ static const safe from modification....

class A
{
...
     static const A zero;
%AccessCode
     return new A(A::zero);
%End
...

But then, it seems likely that these copies will be leaked, so
we seem to be stuck...

Perhaps SIP could support a variable annotation that indicates
that a copy should be passed to Python, rather than a reference,
as an approximation to const in C++.

(For a static member function we would be able to use
  TransferBack to transfer ownership of the copy to Python)

Perhaps more ideally, a const-flavored SIP wrapper object could
omit the non-const methods - resulting in a lighter-weight and
more general purpose solution to the problem.

Nigel Stewart




More information about the PyQt mailing list