Why Python 3?
Gregory Ewing
greg.ewing at canterbury.ac.nz
Sun Apr 20 19:13:23 EDT 2014
Terry Reedy wrote:
> On 4/19/2014 9:06 PM, Gregory Ewing wrote:
>
>> Similarly, when you write // you're explicitly requesting
>> integer division.
>
> One is requesting 'floor division'
>
> >>> 3.0//2.0
> 1.0
In general that's true, but I'm talking about a context
in which you have some expectations as to the types of the
operands.
Most of the time, there are two possible scenarios:
1) The algorithm operates on integers, and the contract is
that you only get passed ints. In that case, you use //
and know that the result will be an int.
2) The algorithm operates on non-integers, and the contract
is that you get passed either ints or floats, with ints being
understood as standing in for floats. In that case, you
use / and know that it will perform float division and
return a float.
If someone passes you a float in case (1) it's true that
// won't return an int, but then they've violated the
contract.
--
Greg
More information about the Python-list
mailing list