Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: What is a function parameter =[] for? Date: Wed, 25 Nov 2015 11:56:45 +1100 Lines: 32 Message-ID: References: <8601c9af-a7d9-4642-ba1c-8edd1e4c3390@googlegroups.com> <56546985.8060704@rece.vub.ac.be> <56547337.4000709@rece.vub.ac.be> <56547C53.8030407@rece.vub.ac.be> <5654864E.2070906@rece.vub.ac.be> <56549324.8020607@rece.vub.ac.be> <56549F15.40206@rece.vub.ac.be> <5654B037.6020107@rece.vub.ac.be> <56550273$0$1585$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de k6t+rRIf216y2JpMzUoNOQoVeJcOm1X0IkQAwDjAeJPw== 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; 'skip:[ 20': 0.03; 'compiler': 0.05; 'none,': 0.05; 'cc:addr:python-list': 0.09; '(1,': 0.09; 'constants.': 0.09; 'immutable': 0.09; 'tuple': 0.09; 'wed,': 0.15; "'a',": 0.16; "'b',": 0.16; "'c',": 0.16; "'d',": 0.16; "'e',": 0.16; 'constants': 0.16; 'displays.': 0.16; 'expression.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'literals,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'skip:{ 20': 0.18; 'language': 0.19; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; "aren't": 0.22; 'tuples': 0.22; 'am,': 0.23; 'sets': 0.23; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'message-id:@mail.gmail.com': 0.27; 'if,': 0.29; 'e.g.': 0.30; 'though,': 0.32; "d'aprano": 0.33; 'smart': 0.33; 'steven': 0.33; 'case,': 0.34; 'lists': 0.34; 'received:google.com': 0.35; 'nov': 0.35; 'something': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'data': 0.39; 'called': 0.40; 'treat': 0.72; 'special': 0.73; 'chrisa': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=XDEZ+DIRZkmvohvfsSZ4jC0Y9+PyiEEaeQFsn77sFro=; b=DCLZAVPAFpdAIWs3Y91D4xMC5xdXR9xJHY1eRqlUd9qDqyIeaK+bO5g8MRHuRi/EH5 ADRCdVHLcY+veS2mZ0O8j0VR1T3KbAuQS3IQbgpg6xGu3XJCiIJC5yZK/HXGJCxjZZMp BxWSfH7y/qotTTplG4Bk2loXjjS7dmVXF9vSTMKHDrTbE4s4A7XPofEB/AQDshAUw8fA b9hBzXrMfBOVNs1jBsbNCSzMuUZ4gtO8O55Bwuswo+uagQNdZADU8olX8r9gmU5Txwbz r7ezuLVXJTTSv3rwqL7eHqojwS3VveDXkPbL3JK+gbbNvUbcv6L4dcMk45niYBy3DK1s ORbg== X-Received: by 10.50.93.72 with SMTP id cs8mr1450594igb.13.1448413005426; Tue, 24 Nov 2015 16:56:45 -0800 (PST) In-Reply-To: <56550273$0$1585$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99408 On Wed, Nov 25, 2015 at 11:36 AM, Steven D'Aprano wrote: > If, and only if, the tuple > contains nothing but immutable constants e.g. > > (1, 2.0, None, "spam") > > then a sufficiently smart compiler may be able to treat that specific tuple > as a constant/literal. But that's a special case, and cannot be generalised > to all tuple displays. In which case it's simply an example of constant-folding. It's the same as (1+2j) being called up with LOAD_CONST, despite being an expression. Recent CPythons are even willing to change what data type something is, to make it a constant: >>> dis.dis(lambda x: x in ["a","b","c","d","e"]) 1 0 LOAD_FAST 0 (x) 3 LOAD_CONST 6 (('a', 'b', 'c', 'd', 'e')) 6 COMPARE_OP 6 (in) 9 RETURN_VALUE >>> dis.dis(lambda x: x in {"a","b","c","d","e"}) 1 0 LOAD_FAST 0 (x) 3 LOAD_CONST 6 (frozenset({'d', 'c', 'a', 'e', 'b'})) 6 COMPARE_OP 6 (in) 9 RETURN_VALUE Lists and sets are definitely not constants. Tuples and frozensets aren't literals, but they can be constant. This is the optimizer talking, though, not language semantics. ChrisA