Как объявить типы для атрибутов класса? - PullRequest
1 голос
/ 01 апреля 2019

Как объявить типы атрибутов класса? Например, в приведенном ниже классе, как объявить self.number как тип unsinged int?

Попытка объявить это так:

class A:    

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

Результатов этой ошибки:

---------------------------------------------------------------------------
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()

Решение другого вопроса, который объявляет тип атрибута в начале определения класса, также терпит неудачу:

class A:
    cdef unsigned int number

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

Это приводит к ошибке ниже:

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()

Как объявить типы атрибутов для класса Python?

...