Immutable and Mutable Types

Stargaming stargaming at gmail.com
Mon Mar 17 11:57:46 EDT 2008


On Mon, 17 Mar 2008 10:40:43 +0000, Duncan Booth wrote:
> Here's a puzzle for those who think they know Python:
> 
> Given that I masked out part of the input, which version(s) of Python
> might give the following output, and what might I have replaced by
> asterisks?
> 
>>>> a = 1
>>>> b = ****
>>>> if a is b: print 'yes!'
> ...
>>>> b
> 1
>>>> type(b)
> <type 'int'>

I know it! Using CPython 2.5.2a0, 2.6a1+ and 3.0a3+::

    >>> b = type('type', (type,), {'__repr__': lambda self:
    ... "<type 'int'>"})('int', (object,), {'__repr__': 
    ... lambda self:"1"})()
    >>> b
    1
    >>> type(b)
    <type 'int'>
    >>> 1 is b
    False

What's my prize?



More information about the Python-list mailing list