擅长:python、mysql、java
<p>当混合<em>类型时,总是会得到'wider'类型,其中复数比float宽,而float又比整数宽。当两个值都是同一类型(所以<em>不是</em>混合类型)时,只会得到一个类型作为结果。你知道吗</p>
<p>从<a href="https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex" rel="nofollow"><em>Numeric Types</em> documentation</a>:</p>
<blockquote>
<p>Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where plain integer is narrower than long integer is narrower than floating point is narrower than complex. Comparisons between numbers of mixed type use the same rule.</p>
</blockquote>
<p>因此,当对不同类型的数字求和时,Python会将较窄的类型加宽,使其与较宽的类型相同。对于带有整数和浮点数的操作,您将得到一个float,因为float更宽。你知道吗</p>
<p>但是,如果两个操作数都是同一类型,则更改类型没有意义。在那种情况下改变类型将是非常令人惊讶的。你知道吗</p>