req: support cdef class attrib initialisation
|Reported by:||rebirth@…||Owned by:||somebody|
|Component:||Code Generation||Keywords:||class attribute initialisation|
A common Python programming style is for classes to be configured via initialised class attributes, which allows users to create subclasses with different initial values, for example:
class Foo: bar = 5 baz = "someval" class MyFoo(Foo): bar = 7 baz = "anotherval"
However, this does not presently work with Cython/Pyrex?.
This filing is a request for support of initialised cdef class attributes, eg:
cdef class Foo: cdef public int bar = 5 cdef public object baz = "someval" ... cdef class MyFoo(Foo): cdef public int bar = 7 cdef public object baz = "anotherval" ... class AnotherFoo(Foo): bar = 8 baz = "yetanotherval"
In this example, we subclass Foo twice - once with an extension subclass containing initialised cdef class attributes, and the other as a pure-python subclass containing initialised class attributes in pure-python syntax.
Ideally, both idioms would be supported. In the former case, the int value 7 would be written to the attribute 'bar' prior to init, and in the latter case, the int object 8 would be converted to a C integer then written to the attribute bar.
The advantage of supporting these cdef class attribute initialisations is that it would allow users to stick with the python idiom of 'initialised class attributes as configuration option', while allowing the speed advantage of struct-level attribute access (as opposed to the slower getattr at the pure-python level).