Do Cython extension types support class attributes?

后端 未结 3 631
遇见更好的自我
遇见更好的自我 2021-01-11 16:55

Python classes can have class attributes:

class Foo(object):
   bar = 4

Is there an analogous construct for defining class attributes in Cy

3条回答
  •  离开以前
    2021-01-11 17:36

    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:

    cdef class Foo:
        bar = 4
    

    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.

提交回复
热议问题