[Tutor] For - if - else loop; print selective output

ALAN GAULD alan.gauld at btinternet.com
Thu Oct 25 02:32:52 CEST 2012


>>> Using "in ['25', '26']" also checks a compiled tuple constant:
>>>
>...
>>> 3.x adds frozenset support:
>>>
>>>      >>> compile("x in {'25', '26'}", '', 'eval').co_consts
>>>      ('25', '26', frozenset({'25', '26'}))
>>
>>
>> I confess I don't know what that means!
>
>Sorry, I was showing that the compiler (at least for CPython) special
>cases "in" and "not in" comparisons when the right-hand operand is a
>list literal of constants. Instead of going to the trouble of building
>a list every time the code runs, it uses a tuple that's stored in the
>code object's co_consts attribute.
>Ah, OK that makes sense now, thanks. Not sure that beginners will 
make much of it but the gist is that Python will optimise the test. :-)

Alan g.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121025/6bba87a6/attachment.html>


More information about the Tutor mailing list