擅长:python、mysql、java
<p>如果您真的对到处使用<code>try/except</code>感到厌烦,请编写一个helper函数:</p>
<pre><code>def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
>>> print RepresentsInt("+123")
True
>>> print RepresentsInt("10.0")
False
</code></pre>
<p>这将是更多的代码来精确地覆盖Python认为是整数的所有字符串。我说在这件事上做点恶毒的事。</p>