Python 如何声明类属性的类型?
如何声明类属性类型?例如,在下面的类中,如何将Python 如何声明类属性的类型?,python,python-3.x,class,cython,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) 导致此错误的原因: ---------------------------
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()
它会导致以下错误:
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()
编译Cython文件时出错:
------------------------------------------------------------
...
A类:
无符号整数
^
------------------------------------------------------------
/home/greg/.cache/ipython/cython/\u cython\u magic\u 48a1f134ce27274c6f5bdb1c47e52.pyx:2:9:cdef语句此处不允许
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
---->1获取\u ipython()。运行\u cell\u magic('cython','',类A:\n cdef无符号整数编号\n\n def \uuuuuuuuu init\uuuuuuuu(self):\n self.number=4\n打印(类型(self.number),self.number)\n')
~/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run\u cell\u magic(self,magic\u name,line,cell)
2321 magic_arg_s=self.var_expand(行、堆栈深度)
2322带自建存水弯:
->2323结果=fn(魔法参数,单元格)
2324返回结果
2325
在cython中(自身、线、单元)
~/anaconda3/lib/python3.7/site-packages/IPython/core/magic.py in(f,*a,**k)
185#但就这一点来说就太过分了。
186 def魔术装饰(arg):
-->187调用=λf,*a,**k:f(*a,**k)
188
189如果可调用(arg):
Cython中的~/anaconda3/lib/python3.7/site-packages/Cython/Build/IpythonMagic.py(self、line、cell)
323如有需要,请:
324 extensions=self.\u cythonize(模块名称、代码、库目录、args、quiet=args.quiet)
-->325断言长度(扩展)==1
326扩展=扩展[0]
327 self.\u code\u cache[key]=模块名称
TypeError:类型为“NoneType”的对象没有len()
如何为Python类声明属性类型?为什么文档()中的第一个示例不是您的选项?@ead该类需要可以从Python访问,
cdef类
似乎不允许这样做OK,您的问题还不清楚。文档中的第二个示例如何:?@ead谢谢,我想这就是答案,在cython中对cdef类进行子类化可能的重复,为什么文档中的第一个示例()不是您的选项?@ead该类需要可以从Python访问,并且cdef类
似乎不允许这样做OK,你的问题不清楚。文档中的第二个示例如何:?@ead谢谢,我想这就是答案,在cython中对cdef类进行子类化可能是的副本
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()