Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!feeder.erje.net!eu.feeder.erje.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; 'python,': 0.02; 'argument': 0.05; 'everybody,': 0.07; 'none:': 0.07; 'arguments': 0.09; 'arguments,': 0.09; 'explanation': 0.09; 'indeed,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '[2,': 0.16; 'container.': 0.16; 'invocations': 0.16; 'mutable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:default': 0.16; 'successive': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'please?': 0.24; 'question': 0.24; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'thus': 0.29; "i'm": 0.30; 'asked': 0.31; 'container': 0.31; 'equivalent.': 0.31; 'subject:the': 0.34; 'anybody': 0.35; 'but': 0.35; 'there': 0.35; 'expected': 0.38; 'mine': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'simple': 0.61; 'such': 0.63; 'results': 0.69; 'default': 0.69; 'acts': 0.74; 'friend': 0.79; 'received:pacbell.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: emile Subject: Re: Modifying the default argument of function Date: Tue, 21 Jan 2014 11:20:15 -0800 References: <52dec646$0$2934$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: adsl-69-226-129-65.dsl.pltn13.pacbell.net User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: <52dec646$0$2934$426a74cc@news.free.fr> 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390332031 news.xs4all.nl 2913 [2001:888:2000:d::a6]:60024 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64446 Function defs with mutable arguments hold a reference to the mutable container such that all invocations access the same changeable container. To get separate mutable default arguments, use: def f(x=None): if x is None: x=[2,3] Emile On 01/21/2014 11:11 AM, Mű wrote: > Hi everybody, > > A friend of mine asked me a question about the following code: > > [code] > def f(x=[2,3]): > x.append(1) > return x > > print(f()) > print(f()) > print(f()) > [/code] > > The results are [2, 3, 1], [2, 3, 1, 1] and [2, 3, 1, 1, 1]. > > The function acts as if there were a global variable x, but the call of > x results in an error (undefined variable). I don't understand why the > successive calls of f() don't return the same value: indeed, I thought > that [2,3] was the default argument of the function f, thus I expected > the three calls of f() to be exactly equivalent. > > I'm don't know much about python, does anybody have a simple explanation > please? >