How to add a static member to a Cython class (from python, not C)

How to add a static, typed element to a cython class? The syntax for adding typed member instances uses the syntax as follows (for example :):

import cython

cdef class NoStaticMembers:

   cdef public int instanceValue # but, how do I create a static member??

   def __init__(self, int value):
       self.instanceValue = value
+4
source share
1 answer

Just because you can do it in C doesn’t really mean you can do it in Cython.

The working environment can use global variables and class properties so that you can access them through class instances. I'm not sure if this is really better than using global variables though

import cython

cdef int MyClass_static_variable

cdef class MyClass:
   property static_variable:
      def __get__(self):
         return MyClass_static_variable

      def __set__(self, x):
         global MyClass_static_variable
         MyClass_static_variable = x

, (, , , __get__ __set__ cpdef, - ). , , , - MyClass.static_variable.

+1

All Articles