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: Tue, 24 Nov 2015 23:07:34 +1100 Lines: 44 Message-ID: References: <56544BAB.9020709@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de AIfWFNnk2Cqra0XUSg4x3A/otPyRJfpSO+EmhgPoSPPQ== 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; 'received:209.85.223': 0.03; 'value,': 0.03; 'defaults': 0.05; 'correct.': 0.07; 'dynamically': 0.07; 'objects,': 0.07; 'cc:addr:python-list': 0.09; 'arg': 0.09; 'integers': 0.09; 'mutable': 0.09; 'python': 0.10; 'def': 0.13; 'value.': 0.15; '(so,': 0.16; '*never*': 0.16; '*values*': 0.16; '24,': 0.16; 'binding,': 0.16; 'executed,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function?': 0.16; 'mutated': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'byte': 0.18; '>>>': 0.20; 'changes': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'object.': 0.22; 'code,': 0.23; 'seems': 0.23; 'represents': 0.23; 'thus': 0.24; 'header:In-Reply- To:1': 0.24; 'example': 0.26; 'sense': 0.26; 'message- id:@mail.gmail.com': 0.27; 'fine': 0.28; 'looks': 0.29; 'behaviour': 0.29; 'second,': 0.29; "i'm": 0.30; 'code': 0.30; 'probably': 0.31; 'certain': 0.31; 'skip:_ 10': 0.32; 'skip:d 40': 0.32; 'statement': 0.32; 'class': 0.33; 'source': 0.33; 'call,': 0.33; 'right?': 0.33; 'values.': 0.33; 'tue,': 0.34; 'lists': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'so,': 0.35; 'skip:c 30': 0.35; 'could': 0.35; 'identity': 0.35; 'nov': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'late': 0.38; 'no,': 0.38; "won't": 0.38; 'received:209': 0.38; 'anything': 0.38; 'why': 0.39; 'rather': 0.39; 'where': 0.40; 'still': 0.40; 'some': 0.40; 'default': 0.61; 'talking': 0.67; 'email,': 0.69; 'now:': 0.72; 'certain,': 0.84; 'chrisa': 0.84; 'pardon': 0.84; 'to:none': 0.91; 'notion': 0.91; 'promised': 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=YgnMNuihW2EL/QcR6LCmT1w7hhecDRqIAgGvDDnTqBo=; b=sqC57GyKSt/iCD07Mz3yb8iYMCw+tbygHgiUippXZ/oNh6q1tvkLmKEudJ4WVkDyOF hORu56iZJBGWydCLm69mNycRNEUPlLo8KTBhgb1APh2b0h2b1yz4qpje/s1xN9Mss8Rf 00lXc/wQ7RePp8sGF5DOVVTRo5t/UQ2PEZDwVnU3m96OjCQqLs+42MYUTFv01piRXRtt 591GMZWSFytBZoCY2NbbM7yOv+sMydf7+uTu3Ib03FJoc7iBAFEG6kVbj0mEgNxrbL1N TC8MQxKAgqxABs3SPRjI4i6to8pl5Uvs6cPfIsuL3RvkBJsLFx9fw39idg2VUXCLRHhs AK5Q== X-Received: by 10.107.10.233 with SMTP id 102mr29237911iok.31.1448366854940; Tue, 24 Nov 2015 04:07:34 -0800 (PST) In-Reply-To: <56544BAB.9020709@rece.vub.ac.be> 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:99320 On Tue, Nov 24, 2015 at 10:36 PM, Antoon Pardon wrote: >>> So, looking at some source code, a default value for certain types is only >>> certain to be that value for the very first call of that function? >> >> On the contrary, it is certain always to be that exact object. > > No, he is talking about the value. Since the object can be mutated > and thus have an other value, his statement seems correct. With mutable objects, you can *never* depend on their values. Any time ANY byte code gets executed, the value could change. That's why I have never said anything about *values* of arg defaults - what you're promised is that the *object* will be the same (so, I'm looking at its identity, rather than its value). So it's not even certain to be that value even for the first call: def create_user(name, email, creation_time=datetime.datetime.now()): Looks perfectly reasonable, right? And with late binding, sure! But with early binding, this will probably NEVER be what you want. But what if you have an object that always stringifies the current time? class Now: def __init__(self, fmt): self.fmt = fmt def __repr__(self): return datetime.datetime.now().strftime(self.fmt) def create_user(name, email, creation_time=Now("%Y%m%d%H%M%S")): Voila! An object whose value dynamically represents the notion of "Now", but where it makes fine sense to have the same object used every time. Since the value changes every second, it (probably) won't even have that value for the first call, yet the behaviour will be correct. His statement is still incorrect, in that the value is NEVER certain, but the identity is ALWAYS certain. It happens to be the case for the one example of lists that get mutated only in the function. It's about as accurate as the statement that Python is pass-by-value for integers but pass-by-reference for dictionaries. ChrisA