<p>取消勾选期间出错,因为在取消勾选期间重新创建对象时,<code>name</code>未准备好传递</p>
<p>它可以通过以下方式复制:</p>
<pre><code>pickle.loads(pickle.dumps(jack))
Traceback (most recent call last):
...
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-9-239857af5731>", line 1, in <module>
pickle.loads(pickle.dumps(jack))
TypeError: __new__() missing 1 required positional argument: 'name'
</code></pre>
<p>解决方案是实现<a href="https://docs.python.org/3/library/pickle.html#object.__getnewargs__" rel="nofollow noreferrer">^{<cd2>}</a>或<a href="https://docs.python.org/3/library/pickle.html#object.__getnewargs_ex__" rel="nofollow noreferrer">^{<cd3>}</a></p>
<blockquote>
<p>object.<strong>getnewargs</strong>()</p>
<p>This method serves a similar purpose as <strong>getnewargs_ex</strong>(), but
supports only positional arguments. It must return a tuple of
arguments args which will be passed to the <strong>new</strong>() method upon
unpickling.</p>
<p><strong>getnewargs</strong>() will not be called if <strong>getnewargs_ex</strong>() is defined.</p>
<p>Changed in version 3.6: Before Python 3.6, <strong>getnewargs</strong>() was called
instead of <strong>getnewargs_ex</strong>() in protocols 2 and 3.</p>
</blockquote>
<p>因此,在你的情况下:</p>
<pre><code>def __getnewargs__(self):
return self.name,
</code></pre>