Why Python 3?
Terry Reedy
tjreedy at udel.edu
Sun Apr 20 20:09:25 EDT 2014
On 4/20/2014 7:13 PM, Gregory Ewing wrote:
> 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
The name 'floor division' and the float result are intentional, not
accidents.
> 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.
Not necessarily if the float has an integer value. The intention of the
change was to make the value of number operations less dependent on the
type of the operands. Where the result type does matter is if the result
is used, for example, in indexing
--
Terry Jan Reedy
More information about the Python-list
mailing list