Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'position,': 0.05; 'subsequent': 0.05; 'assignment': 0.07; 'defaults': 0.07; 'none:': 0.07; '%s"': 0.09; 'created,': 0.09; 'cursor': 0.09; 'parameter': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'assume': 0.14; 'useful,': 0.14; ';-)).': 0.16; 'anyways,': 0.16; 'argument.': 0.16; 'called.': 0.16; 'defined.': 0.16; 'dictionary,': 0.16; 'discarded.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'mutable': 0.16; 'optional': 0.16; 'subject:parameters': 0.16; 'third,': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'meant': 0.20; 'example': 0.22; 'cc:addr:python.org': 0.22; 'decide': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'gets': 0.27; 'header :In-Reply-To:1': 0.27; 'function': 0.29; 'michael': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'went': 0.31; 'default,': 0.31; 'file': 0.32; 'another': 0.32; 'quite': 0.32; 'fri,': 0.33; 'could': 0.34; "can't": 0.35; 'common': 0.35; 'something': 0.35; 'etc': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'transition': 0.36; 'doing': 0.36; 'two': 0.37; 'list': 0.37; 'project': 0.37; 'easily': 0.37; 'form,': 0.38; 'needed': 0.38; 'pm,': 0.38; 'anything': 0.39; 'expect': 0.39; 'though,': 0.39; 'sure': 0.39; 'most': 0.60; 'new': 0.61; "you're": 0.61; 'first': 0.61; 'times': 0.62; 'show': 0.63; 'different': 0.65; 'default': 0.69; 'to,': 0.72; 'brand': 0.72; 'construction': 0.72; '2015': 0.84; 'common,': 0.84; 'dict,': 0.84; 'idiom': 0.84; 'welle': 0.84; 'to:none': 0.92; 'incredibly': 0.96 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=pgO/fA8xzOjYvnObg7KlgUT48K1PrK69UWCxlC8ofnM=; b=XvrcCCAjwa5/JTL2wWMw8YbU/xGKJVA5Y5QFpYI5g2TrFqYXcjXaElsWhYCLIEBaz+ LqV3S2zBAZTrDSATtwigkso43F3a6twyx87zvUgZGzg7CIAP15gp9b1pQ1KCmmQlqcd6 Ut6kxvKZFSNW1gFcDXof0YljLmGdIwzEWMT8tvnKaaVC5zatsv+xjbnWWvTUlRxYvvSR aCcioM3jgIhWgTGa1XmKkRpLI6QRigy8Nz8LfvzALr54+tv5yPJeab24AHirTjjueOhT 3YDZPXZkSyveqj/SPbExrpgTuzsNJCl/8wABQWg4Rt/Isxv9Hm0YNEWezNi8nyqcbxxb WO/g== MIME-Version: 1.0 X-Received: by 10.107.134.206 with SMTP id q75mr4489174ioi.27.1431088757871; Fri, 08 May 2015 05:39:17 -0700 (PDT) In-Reply-To: <72lu1cxvmg.ln2@news.c0t0d0s0.de> References: <72lu1cxvmg.ln2@news.c0t0d0s0.de> Date: Fri, 8 May 2015 22:39:17 +1000 Subject: Re: functions, optional parameters From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431088766 news.xs4all.nl 2860 [2001:888:2000:d::a6]:48238 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90161 On Fri, May 8, 2015 at 9:59 PM, Michael Welle wrote: > Hello, > > assume the following function definition: > > def bar(foo = []): > print("foo: %s" % foo) > foo.append("foo") > > It doesn't work like one would expect (or as I would expect ;-)). As I > understand it the assignment of the empty list to the optional parameter > foo take place when the function object is created, not when it is > called. I think from the perspective of a user this is very strange. > Anyways, what would be a good idiom to get the desired behaviour? I'm not sure what the desired behaviour _is_, given that you're not doing anything with the list after appending to it. But argument defaults are evaluated when the function's defined. It's basically like this: DEFAULT_FOO = [] def bar(foo | nothing): if no argument passed: foo = DEFAULT_FOO print("foo: %s" % foo) foo.append("foo") There are times when this is incredibly useful, but if you don't want this behaviour, the most common idiom is this: def bar(foo=None): if foo is None: foo = [] print("foo: %s" % foo) foo.append("foo") As an example of this, a project I'm involved in had a transition function which could work in one of two modes: 1) Transition one file into another 2) Transition one file into another, then that into a third, then a fourth, etc In order to maintain state cleanly, a dictionary was passed in, which provided a "previous position" marker, which could then be updated. For the first form, though, all I needed to do was give it an empty dictionary, which would then be discarded. So the function went something like this: def transition(from, to, state=None): if not state: state={"cursor": 0} # ... state["cursor"] = last_position (I can't easily show you the code; subsequent edits meant that the first case actually wanted to retrieve the cursor position, so it now always has a state dict, and has no default argument. But this is still a valid concept.) Both idioms are very common, and you need to decide whether you want a single mutable default, or a None default that gets replaced by a brand new dict/list/etc every time. Just remember that the construction of a new list is quite different from the assignment of a pre-existing list to a new name. ChrisA