Immutable and Mutable Types

Duncan Booth duncan.booth at invalid.invalid
Tue Mar 18 05:47:56 EDT 2008


Stargaming <stargaming at gmail.com> wrote:

> On Mon, 17 Mar 2008 16:03:19 +0000, Duncan Booth wrote:
> 
>> For the answer I actually want each asterisk substitutes for exactly one
>> character.
> 
> Played around a bit and found that one:
> 
> Python 3.0a3+ (py3k:61352, Mar 12 2008, 12:58:20)
> [GCC 4.2.3 20080114 (prerelease) (Debian 4.2.2-7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> a = 1
>>>> b = 1//1
>>>> if a is b: print('yes!')
> ...
>>>> b
> 1
>>>> type(b)
><type 'int'>

I've had a look to see why this happens: long division (and in Python 3 all 
integers are longs) allocates a new long to hold the result of the division 
so it will never use one of the preallocated 'small int' values.

That makes sense so far as it goes, but I'm slightly suprised if it isn't 
worth an extra check somewhere for numerator fitting in a machine int and 
shortcutting the long division.



More information about the Python-list mailing list