如何声明类属性的类型?

时间:2019-04-01 06:38:14

标签: python python-3.x class cython

如何声明类属性类型?例如,在下面的类中,如何将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()

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)

它导致以下错误:

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类声明属性类型?

0 个答案:

没有答案
相关问题