擅长:python、mysql、java
<blockquote>
<p>Does this mean once you define a python class with cdef all self.* attributes have to be cdef defined?</p>
</blockquote>
<p>是的。这在<a href="https://cython.readthedocs.io/en/latest/src/tutorial/cdef_classes.html" rel="noreferrer">the documentation</a>中有明确的表述:</p>
<blockquote>
<p>Attributes in cdef classes behave differently from attributes in regular classes:</p>
<ul>
<li>All attributes must be pre-declared at compile-time</li>
<li>...</li>
</ul>
</blockquote>
<p>通过将属性定义为object类型,可以很好地存储字符串:</p>
<pre><code>cdef public object msg
</code></pre>
<hr/>
<p>在内部,这是因为<code>cdef class</code>没有字典,这样可以节省空间并加快属性访问速度,但这确实意味着它不能在运行时添加任意属性。这相当类似于在普通Python类中使用<code>__slots__</code>。在</p>