1

How to declare class attribute types? For example, in the below class, how to declare self.number as an unsinged int type?

Attempting to declare it this way:

class A:    

    def __init__(self):
        cdef unsigned int self.number
        self.number = 4
        print(type(self.number), self.number)

Results in this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-80176274b668> in <module>
----> 1 get_ipython().run_cell_magic('cython', '', 'class A:    \n    \n    def __init__(self):\n        cdef unsigned int self.number\n        self.number = 4\n        print(type(self.number), self.number)\n')

~/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2321             magic_arg_s = self.var_expand(line, stack_depth)
   2322             with self.builtin_trap:
-> 2323                 result = fn(magic_arg_s, cell)
   2324             return result
   2325 

<decorator-gen-127> in cython(self, line, cell)

~/anaconda3/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/anaconda3/lib/python3.7/site-packages/Cython/Build/IpythonMagic.py in cython(self, line, cell)
    323         if need_cythonize:
    324             extensions = self._cythonize(module_name, code, lib_dir, args, quiet=args.quiet)
--> 325             assert len(extensions) == 1
    326             extension = extensions[0]
    327             self._code_cache[key] = module_name

TypeError: object of type 'NoneType' has no len()

A solution to another question, which declares the attribute type at the top of the class definition also fails:

class A:
    cdef unsigned int number

    def __init__(self):
        self.number = 4
        print(type(self.number), self.number)

It results in the below error:

Error compiling Cython file:
------------------------------------------------------------
...
class A:
    cdef unsigned int number
        ^
------------------------------------------------------------

/home/greg/.cache/ipython/cython/_cython_magic_48a1f134ce2732274c6f5bdb1c477e52.pyx:2:9: cdef statement not allowed here

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-0af4c38224f5> in <module>
----> 1 get_ipython().run_cell_magic('cython', '', 'class A:\n    cdef unsigned int number\n    \n    def __init__(self):\n        self.number = 4\n        print(type(self.number), self.number)\n')

~/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2321             magic_arg_s = self.var_expand(line, stack_depth)
   2322             with self.builtin_trap:
-> 2323                 result = fn(magic_arg_s, cell)
   2324             return result
   2325 

<decorator-gen-127> in cython(self, line, cell)

~/anaconda3/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/anaconda3/lib/python3.7/site-packages/Cython/Build/IpythonMagic.py in cython(self, line, cell)
    323         if need_cythonize:
    324             extensions = self._cythonize(module_name, code, lib_dir, args, quiet=args.quiet)
--> 325             assert len(extensions) == 1
    326             extension = extensions[0]
    327             self._code_cache[key] = module_name

TypeError: object of type 'NoneType' has no len()

How to declare attribute types for a Python class?

Greg
  • 8,175
  • 16
  • 72
  • 125
  • Why is the first example from the documentation (https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#introduction) not an option for you? – ead Apr 01 '19 at 07:03
  • @ead The class needs to be accessible from Python and`cdef class` doesn't seem to allow that – Greg Apr 01 '19 at 07:06
  • Ok, that was not clear from your question. What about the second example in the documentation: https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#static-attributes? – ead Apr 01 '19 at 07:08
  • @ead Thank you, think that's the answer, subclassing the cdef class in cython – Greg Apr 01 '19 at 07:20
  • 3
    Possible duplicate of [Mixing cdef and regular python attributes in cdef class](https://stackoverflow.com/questions/42632297/mixing-cdef-and-regular-python-attributes-in-cdef-class) – ead Apr 12 '19 at 11:06

0 Answers0