擅长:python、mysql、java
<p>目前,解析整数(如<code>"i"</code>)是接受bool的公认方法。</p>
<p>从Python 3.3中,<code>PyArg_ParseTuple</code>将接受<code>"p"</code>(对于“谓词”),每<a href="http://hg.python.org/cpython/file/05274ab06182/Misc/NEWS" rel="nofollow noreferrer">the latest NEWS</a>:</p>
<blockquote>
<ul>
<li><a href="http://bugs.python.org/issue14705" rel="nofollow noreferrer">Issue #14705</a>: The PyArg_Parse() family of functions now support the 'p' format
unit, which accepts a "boolean predicate" argument. It converts any Python
value into an integer--0 if it is "false", and 1 otherwise.</li>
</ul>
</blockquote>
<p>注意,与<code>"p"</code>一起使用<code>PyArg_ParseTuple</code>时,参数必须是(指向)<code>int</code>的指针,而不是C99<code>bool</code>类型:</p>
<pre><code>int x; // not "bool x"
PyArg_ParseTuple(args, kwds, "p", &x);
</code></pre>