Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8479
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <enalicho@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'parameter': 0.05; 'python': 0.08; '>>>>': 0.09; 'foo': 0.09; 'none:': 0.09; 'subject:parameters': 0.09; 'wrote:': 0.15; 'a()': 0.16; 'called,': 0.16; 'executed.': 0.16; '\xa0print': 0.16; 'argument': 0.16; 'cc:addr:python-list': 0.16; 'pm,': 0.16; 'def': 0.16; '(i.e.': 0.17; 'marc': 0.19; 'received:74.125.82.44': 0.19; 'received:mail-ww0-f44.google.com': 0.19; 'cc:2**0': 0.21; 'pointed': 0.22; 'cc:no real name:2**0': 0.22; 'header:In-Reply- To:1': 0.22; 'trying': 0.23; 'default,': 0.23; 'optional': 0.23; 'function': 0.26; "i'm": 0.27; 'message-id:@mail.gmail.com': 0.28; 'lists': 0.29; 'problem': 0.29; 'fix': 0.29; 'cc:addr:python.org': 0.30; 'sun,': 0.30; 'taken.': 0.30; 'thanks': 0.30; 'hi,': 0.30; 'subject:?': 0.31; 'values': 0.31; 'list': 0.33; 'does': 0.33; 'instead': 0.34; 'points': 0.34; 'however,': 0.34; '...': 0.34; 'uses': 0.35; "isn't": 0.35; 'define': 0.35; '(by': 0.37; 'received:google.com': 0.37; 'takes': 0.38; 'subject:: ': 0.38; 'should': 0.39; '[1]': 0.39; 'received:74.125.82': 0.39; 'list,': 0.39; 'received:74.125': 0.40; 'your': 0.61; '26,': 0.67; 'jun': 0.67; 'subject:value': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type:content-transfer-encoding; bh=qe0x5ZYkVjFR7ppWpMk5s1l1kvYaGxNaIqzpIxeAu6k=; b=gWOmW+114+iFkfaKFEcb0T/fy4Km707UUO1xDHN+Wwyub/jaKCvhHCpc6RY6h/YgQx IXYrKAnh+w1agyQUjzzfzIhh62ZiZ3WSrFdx2TUF+VZN5H8SAEpc9uXBF4UwU74uG5gm H8qwBuNPejpBQLRPrVebmeW8HbqXMzt9OU7hg= |
| DomainKey-Signature | a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=kevHtLLhP8iq4Ob/q/UzD9FjvCJrsspkRyFjZovkAPRWWK2wZHTik8AE7eDidw5Lqg X+NqUzLY8GTAm64IASrYX2v7FVE9pED9rq3PnolgMmTqv+y1XhwyspbuVHhOVJD1F7Fl 9HD7Wy3mgNA4kGw5KM1cu4gnD+Nl5D7CaJ1Gk= |
| MIME-Version | 1.0 |
| In-Reply-To | <2a25a54a-c60b-4811-9c6a-97c7f717dccb@hd10g2000vbb.googlegroups.com> |
| References | <2a25a54a-c60b-4811-9c6a-97c7f717dccb@hd10g2000vbb.googlegroups.com> |
| From | Noah Hall <enalicho@gmail.com> |
| Date | Sun, 26 Jun 2011 19:45:11 +0100 |
| Subject | Re: Default value for optional parameters unexpected behaviour? |
| To | Marc Aymerich <glicerinu@gmail.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.427.1309113933.1164.python-list@python.org> (permalink) |
| Lines | 36 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1309113933 news.xs4all.nl 4362 [::ffff:82.94.164.166]:58913 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:8479 |
Show key headers only | View raw
On Sun, Jun 26, 2011 at 7:28 PM, Marc Aymerich <glicerinu@gmail.com> wrote:
> Hi,
> I'm trying to define a function that has an optional parameter which
> should be an empty list whenever it isn't given. However, it takes as
> value the same value as the last time the function was executed. What
> is the reason of this behaviour? How does python deal with default
> values (i.e. when are they assigned/created)?
>
> Thanks :)
>
>>>> def a(foo=[]):
> ... foo.append(1)
> ... print foo
> ...
>>>> a()
> [1]
>>>> a()
> [1, 1]
>>>> a()
> [1, 1, 1]
>>>> a()
> [1, 1, 1, 1]
>>>> a()
> [1, 1, 1, 1, 1]
>>>> a()
> [1, 1, 1, 1, 1, 1]
Your problem arises because lists are mutable. Because foo (by
default, initially) points to a given list, every time the function is
called, it uses the same list that foo was first pointed to, if the
default argument value is taken.
The way to fix this is to instead do -
def a(foo=None):
if foo is None:
foo = []
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Default value for optional parameters unexpected behaviour? Marc Aymerich <glicerinu@gmail.com> - 2011-06-26 11:28 -0700 Re: Default value for optional parameters unexpected behaviour? Shashank Singh <shashank.sunny.singh@gmail.com> - 2011-06-27 00:09 +0530 Re: Default value for optional parameters unexpected behaviour? Corey Richardson <kb1pkl@aim.com> - 2011-06-26 14:36 -0400 Re: Default value for optional parameters unexpected behaviour? Noah Hall <enalicho@gmail.com> - 2011-06-26 19:45 +0100 Re: Default value for optional parameters unexpected behaviour? Terry Reedy <tjreedy@udel.edu> - 2011-06-26 14:46 -0400 Re: Default value for optional parameters unexpected behaviour? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-06-26 12:21 -0700 Re: Default value for optional parameters unexpected behaviour? Corey Richardson <kb1pkl@aim.com> - 2011-06-26 15:23 -0400
csiph-web