Immutable and Mutable Types
Duncan Booth
duncan.booth at invalid.invalid
Mon Mar 17 12:12:13 EDT 2008
Stargaming <stargaming at gmail.com> wrote:
> 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?
Ok, that one is impressive, I should have thought of putting some more
checks (like asserting type(a) is type(b)), but then the whole point of
setting the puzzle was that I expected it to be ripped to shreds. You
can queue up behind Steven to choose any of the exciting prizes on my
non-existent prize table.
Meanwhile, I'll fall back on the same quibbling getout I used with
Steven: too long.
More information about the Python-list
mailing list