Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64444
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.009 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'argument': 0.05; 'none:': 0.07; '22,': 0.09; 'indeed,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; 'stored': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mutable': 0.16; 'other,': 0.16; 'sense,': 0.16; 'subject:default': 0.16; 'successive': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'wed,': 0.18; 'cc:addr:python.org': 0.22; 'error': 0.23; 'initial': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'thus': 0.29; 'message- id:@mail.gmail.com': 0.30; 'equivalent.': 0.31; 'cases': 0.33; 'subject:the': 0.34; 'common': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'list': 0.37; 'expected': 0.38; 'is.': 0.60; 'most': 0.60; 'new': 0.61; 'simply': 0.61; 'results': 0.69; 'default': 0.69; 'acts': 0.74; '6:11': 0.84; 'idiom': 0.84; 'contents.': 0.91; 'to:none': 0.92 |
| 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:content-transfer-encoding; bh=XC/rvvTX3EAdzkuJFimhlQwwKeSIRXfgnpeeCdaKerg=; b=Srr4CPwo5NYUi8t6S4AEyRnqu41ZdtR4vRTsD5sMFMEvgx2m7WS4LWjPqZvbgtyVnM J8WDvXUwNTaT0KFAvMK8+dPDQ6YrUoTuZ8BYTx5wDfVBrbyZ+dDc4LCzxFORQFmTw74k nmmqwJNt5HvVud87d8mR5i6RmlCRY7tTFc4ss8ChGj7Sqn2jY5INDOPnkS6v81W8jJ/+ uGNANU1XPcBSyLJf8x7DdmgzfGicHYdbTafRNQFNF3OR6D8jh9JiCc3FhVcMPfK5e21c abguuWLUpSPXNZQq2CQWwHoC/0QW0qFhU4R4vGIcurBK8bcfwEPobgDg3+pXqokkPlre eOGw== |
| MIME-Version | 1.0 |
| X-Received | by 10.66.118.71 with SMTP id kk7mr26602626pab.14.1390331952892; Tue, 21 Jan 2014 11:19:12 -0800 (PST) |
| In-Reply-To | <52dec646$0$2934$426a74cc@news.free.fr> |
| References | <52dec646$0$2934$426a74cc@news.free.fr> |
| Date | Wed, 22 Jan 2014 06:19:12 +1100 |
| Subject | Re: Modifying the default argument of function |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| 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.5813.1390331956.18130.python-list@python.org> (permalink) |
| Lines | 23 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1390331956 news.xs4all.nl 2955 [2001:888:2000:d::a6]:57508 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:64444 |
Show key headers only | View raw
On Wed, Jan 22, 2014 at 6:11 AM, Mû <mu--@melix.net> wrote:
> 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.
In a sense, there is. The default for the argument is simply an object
like any other, and it's stored in one place.
For cases where you want a mutable default that is "reset" every time,
the most common idiom is this:
def f(x=None):
if x is None: x=[2,3]
x.append(1)
return x
That will create a new list every time, with the same initial contents.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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