Python classes can have class attributes:
class Foo(object):
bar = 4
Is there an analogous construct for defining class attributes in Cython extension types? For example, when I try to compile the following cython code
cdef class Foo:
cdef int bar
bar = 4
I get this error:
thing.c:773:3: error: use of undeclared identifier 'bar'
bar = 4;
^
1 error generated.
error: command 'cc' failed with exit status 1
While it doesn't seem to be possible to have C-typed static attributes, Cython extension types can have regular Python static attributes which are also automatically accessible in Python. Just declare them as you would declare them in Python:
The generated code shows that these static attributes are stored as Python objects in the attribute dict of the class object, i.e. if you use them in contexts where C-types are used, they are converted back from Python objects.
The short answer is yes and no.
No, there is not a convenient syntactic idiom for quickly inserting a class attribute in a
cdef class
. However ....The whole point of
cython
is that it gives you lower level access. The usual motive for the extra effort is performance, but you can also doC
-like things with the extra freedom. The difficulty is, there are many pitfalls, and in this case, you won't get purepython
class attributes without a lot of work. It is nevertheless pretty easy to get what you need for simple use cases.For example, suppose I'm making some calculating engine as a class and I wish to globally set the precision of the return value for all instances. I want a default at compile time, and from time to time I may want to adjust it lower to quickly process some trials, and then adjust it higher for my final work. A class attribute is made to order, but you can get the functionality you need in
cython
as follows:First, define at the module level the following:
Using an array permits us to use the
cython
idiomprecision[0]
which is equivalent to the C*precision
. Thecdef
nameprecision
is implicitly a pointer since the data item is an array. This permits usingcython
syntactic idioms to convert fromcython
storage locations to python references. If all you want is a global constant that may be accessed bycdef
code in any of the classes in the module, you are done. If you want to use it strictly as a class attribute, you must enforce that discipline - the compiler doesn't care.Now if you also want to adjust the value from
python
code, you will need a pair ofcdef
functions thatpython
code in the module can call to access the 'attribute':At this point, the semantics will vary a bit from pure
python
, unless you really want to sweat. You need apython
setter and getter function, and I find thepython
descriptor protocol implemented by properties is easiest:The first gets a python reference to the 'attribute'. The second sets the 'attribute' with a
python
integral value, policed to fall within limits. Thecython
function call and return interface automatically takes care of conversions, which are more complex than they look.For example,
get_precision
returns aC-pointer
. If you did the dereferencing inget_precision
you would get an error trying to return aC-int
in__get__
as if it werepython
. If instead you just omitted the[0]
dereference in__get__
you would get an error trying to return aC-pointer
as if it were apython int
. As written, automatic conversions correctly match types.cython
is very finicky about this sort of thing, and can silently return incorrect values, discoverable only at runtime. It can take some experimentation to infer the correct incantation.The docstring tells you not to expect a pure
python
class attribute. If you want to sub-class, and have sub-classes use a different global setting, you will need to sweat a bit more. Inpython
, all that is done automatically.Even so, there are other differences. A real class attribute may be referenced on the class or on an instance. This property may only be referenced on an instance. Setting a real class attribute on the instance creates an instance specific copy, leaving the class attribute untouched, but invisible to the altered instance.
For the given use case, this works. A real class attribute is unnecessary. Since
cython
code is usually less abstract and compute intensive, this minimal approach is often enough.You can't do that that way. I don't know whether static attributes are supported, but the "normal" ones have to be accessed from methods, e.g. a constructor: