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: Sat, 21 Nov 2015 00:04:03 +1100 Lines: 45 Message-ID: References: <564dbe6b$0$1610$c3e8da3$5496439d@news.astraweb.com> <564df258$0$1604$c3e8da3$5496439d@news.astraweb.com> <564e71f6$0$1619$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de afS6Qqeq5j+5cihtbVDeSw8HIx8SaL37RLGo3rKoq/YQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'sys': 0.05; 'variable,': 0.07; 'cc:addr:python-list': 0.09; '*value*': 0.09; 'defined,': 0.09; 'mutable': 0.09; 'objects.': 0.09; 'to)': 0.09; 'python': 0.10; 'syntax': 0.13; 'def': 0.13; 'argument': 0.15; '(barring': 0.16; '(empty': 0.16; '*expression*': 0.16; 'changes!': 0.16; 'constructs': 0.16; 'evaluates': 0.16; 'evaluating': 0.16; 'former,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'omitted.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'syntactic': 0.16; 'wrote:': 0.16; 'element': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'object.': 0.22; 'changes,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'object,': 0.27; 'function': 0.28; 'probably': 0.31; "can't": 0.32; 'definition': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'nov': 0.35; "isn't": 0.35; 'but': 0.36; 'list,': 0.36; 'instead': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'assigned': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'does': 0.39; 'some': 0.40; 'default': 0.61; 'face': 0.64; '20,': 0.66; 'virtually': 0.66; 'results': 0.66; 'as:': 0.79; '*when': 0.84; 'chrisa': 0.84; 'to:none': 0.91; 'acknowledge': 0.93 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=/BC12Mh4t6/7ywAUzNP2vfkYUzwtIG/RNIvWB38JKNA=; b=gePIeT5RB74r4AO6uwlKytwaBx113Cec0Zxl5PDVUaLgLyxKX1uixAnBUUt2sdW2Ap Kw8e32mYXyNMl/YZVYKl6jqzwyV9b2vkddAM77MwVqIBfNI8lhDVR/lTStaJJEVZbjtb i4p3S4v9VNDpKU8amkmguzHfO3C5fre7P9+SE5VDoH2gHmLJ9MWw1em0UPklPhh4FNS7 dt8eY7ykrUsFArAfxv1OUhr/8dpl/Z9ATOh+1cQ5enfshi4gW3xT1l8wVUiE0etBGfsA EwhyFZQ7uYVlF3PN31Kmpb1fcTwRDjZBJ7VF4TSDG4EsjVa00/zyFoVcXfkm6XIKWThu +O7Q== X-Received: by 10.50.83.38 with SMTP id n6mr1718405igy.92.1448024644103; Fri, 20 Nov 2015 05:04:04 -0800 (PST) In-Reply-To: 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:99143 On Fri, Nov 20, 2015 at 11:39 PM, BartC wrote: > * The refusal to acknowledge that the def fn(a=[]) syntax is misleading. > (What value will a have when you call fn()? The true answer is that you > can't tell.) It isn't misleading. The default value for the argument is set when the function is defined, and it is set to the *object* that results from evaluating the *expression* given. After that, the *object* is the one you will always get (barring shenanigans) if the argument is omitted. If the value of that object changes, it changes! You keep expecting the *value* to be consistent. But what you actually get is that the *object* is consistent. It's virtually impossible to guarantee the former, in the face of mutable objects. > * The persistent nonsense that somehow [] is mutable (what happens is that > [] is assigned to a variable, and /that/ is mutable) (And I will probably > get some flak now because 'assign' and 'variable' are meaningless in > Python!) What happens is that [] evaluates to an object, and *that object* is mutable. Python does not store the syntactic element "[]" (empty list-display), but instead stores (a reference to) the object. It's exactly the same as: def fn(a=list()): ... def generate_list(): return [] def fn(a=generate_list()): ... initial_list = [] def fn(a=initial_list): ... import sys def fn(a=[x*3+424 for x in range(not sys)]): """how ridiculous would you like to go?""" Every one of these constructs exactly one new list, *when it is evaluated*, which is at function definition time. ChrisA