python - 如何声明类属性的类型?
问题描述
如何声明类属性类型?例如,在下面的类中,如何声明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 类声明属性类型?
解决方案
推荐阅读
- c# - 我想在天蓝色搜索中对日期范围执行时间范围搜索
- r - quasi element wise matrix multiplication in R
- java - Java-8 获取特定范围内的值的计数
- amazon-web-services - 识别垂直对齐的数字?
- kubernetes - Kubernetes pod 中的磁盘使用情况
- excel - VBA:如何在单个折线图上运行仅 Y 轴的最小/最大比例宏?
- debugging - Xamarin Forms - iOS CLLocationCoordinate2D 未正确初始化
- python - How do I dynamically instantiate a class whose constructor takes multiple parameters?
- android - Android 问:从 BroadcastReceiver 的 Android 10 上的后台任务启动活动?
- c++11 - 如何仅在 Windows 7 上禁用 Aero Shake 最小化