Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'compiler': 0.05; 'cpython': 0.05; 'received:134': 0.05; 'python': 0.09; 'arg': 0.09; 'immutable': 0.09; 'literal': 0.09; 'mutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:while': 0.09; 'wastes': 0.09; 'def': 0.10; 'echo': 0.16; 'literals': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.17; 'creates': 0.18; 'memory': 0.18; '3.2': 0.22; 'constant': 0.22; 'minor': 0.22; 'object.': 0.22; 'sets': 0.23; 'testing': 0.24; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'creating': 0.26; 'common': 0.26; 'header:X-Complaints-To:1': 0.28; 'skip:( 20': 0.28; "d'aprano": 0.29; 'steven': 0.29; 'to:addr:python-list': 0.33; 'membership': 0.33; 'there': 0.35; 'received:org': 0.36; 'created': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'most': 0.61; 'special': 0.73; 'sets,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Serhiy Storchaka Subject: Re: Implicit conversion to boolean in if and while statements Date: Fri, 15 Feb 2013 21:09:42 +0200 References: <86872ad2-fda0-403b-9f18-d1cb18e41860@t32g2000yqd.googlegroups.com> <50039290$0$29978$c3e8da3$5496439d@news.astraweb.com> <9309333c-13a0-464c-bd94-9c682363b8c9@googlegroups.com> <511516db$0$29969$c3e8da3$5496439d@news.astraweb.com> <62c3e7bb-d023-43b4-b759-f424707fd346@googlegroups.com> <75c82449-773e-4077-a6c9-e9cef08f845f@googlegroups.com> <13e5e306-d253-418e-a1b2-ac5bde03f07d@googlegroups.com> <5117868b$0$29998$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 134.249.80.25 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 In-Reply-To: <5117868b$0$29998$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360955401 news.xs4all.nl 6847 [2001:888:2000:d::a6]:42079 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38949 On 10.02.13 13:37, Steven D'Aprano wrote: > Unfortunately, Python has a minor design flaw. One of the most common > use-cases for sets is for membership testing of literal sets: > > def example(arg): > if arg in {'spam', 'ham', 'eggs', 'cheese'}: > ... > > Unfortunately, set literals create *mutable* sets, not frozensets. That > means that the compiler wastes time and memory creating am over-allocated > mutable set object. If set literals created immutable frozensets, there > would be some nice opportunities for the compiler to optimize this > use-case. CPython 3.2 optimizes this special case and creates a constant frozenset. $ echo "arg in {'spam', 'ham', 'eggs', 'cheese'}" | python3.2 -m dis - 1 0 LOAD_NAME 0 (arg) 3 LOAD_CONST 5 (frozenset({'cheese', 'eggs', 'ham', 'spam'})) 6 COMPARE_OP 6 (in) 9 POP_TOP 10 LOAD_CONST 4 (None) 13 RETURN_VALUE