Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64446

Re: Modifying the default argument of function

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 <python-python-list@m.gmane.org>
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 <emile@fenx.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5814.1390332031.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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?
>

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Modifying the default argument of function Mû <mu--@melix.net> - 2014-01-21 20:11 +0100
  Re: Modifying the default argument of function Chris Angelico <rosuav@gmail.com> - 2014-01-22 06:19 +1100
    Re: Modifying the default argument of function Mû <mu--@melix.net> - 2014-01-21 20:36 +0100
      Re: Modifying the default argument of function Chris Angelico <rosuav@gmail.com> - 2014-01-22 06:46 +1100
        Re: Modifying the default argument of function Asaf Las <roegltd@gmail.com> - 2014-01-21 16:30 -0800
  Re: Modifying the default argument of function Steve Jones <steve@secretvolcanobase.org> - 2014-01-21 19:19 +0000
  Re: Modifying the default argument of function emile <emile@fenx.com> - 2014-01-21 11:20 -0800

csiph-web