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


Groups > comp.lang.python > #69612

Re: Default mutable parameters in functions

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'function:': 0.09; 'parameter': 0.09; 'def': 0.12; "wouldn't": 0.14; 'expecting': 0.16; 'foo()': 0.16; 'picks': 0.16; 'subject:parameters': 0.16; 'substituted': 0.16; 'value;': 0.16; 'wrote:': 0.18; 'thu,': 0.19; "haven't": 0.24; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; 'you?': 0.31; 'call.': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'back': 0.62; "you've": 0.63; 'different': 0.65; 'default': 0.69
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=hezTXbRlIaDLyNaBoKcPaPTXCkQk36YrBnmRRi+fRyY=; b=txxgIXJwcL+Gd6ZGyKFLGhfyEEgFj4FmeqV5oEluZSdbB8DzkYfb2tArt2ijniV42w ojlYbO/Ue5f+vLge+CmHGOdK8kp9h6wvFLGJKwUp4VqXMjdoPPI2usN/GfHKb4Hbph8W KXlmeZWn46o7jV9BxxkzxYWhMivimUAjRnnBviPryu91KQURacSh9W6zEGhYSd6rLEhd SGC1zbEDcOhn59b8C6otY0U+9oOYbkLpaLWgemmmzwJ9/kJCJVFThV56Cthybai5qbOp Vq3z0S5Fw15XqiYQx0rOYSgnxy2zoYberQnyNPOevm/T6G5ipnMk0P0AJqM7awo4UOrh t2JA==
X-Received by 10.68.110.165 with SMTP id ib5mr10321892pbb.61.1396560802220; Thu, 03 Apr 2014 14:33:22 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <ac388f0b-8951-42af-a94f-33abdb7231e7@googlegroups.com>
References <ac388f0b-8951-42af-a94f-33abdb7231e7@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 3 Apr 2014 15:32:42 -0600
Subject Re: Default mutable parameters in functions
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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.8857.1396560805.18130.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396560805 news.xs4all.nl 2891 [2001:888:2000:d::a6]:40671
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69612

Show key headers only | View raw


On Thu, Apr 3, 2014 at 12:49 PM,  <fbicknel@gmail.com> wrote:
> Now call it with a value:
> foo([ 3 ])
>
> as you might expect:
> It's a parrot, not a cheese: [3]
>
> But now go back to no parameter in the call:
> foo()
> foo()
> foo()
>
> It's a parrot, not a cheese: [46]
> It's a parrot, not a cheese: [47]
> It's a parrot, not a cheese: [48]
>
> it picks up where it left off.
>
> I was rather expecting it to start with 4!

You haven't replaced the default value; you've only substituted a
different value for that call.  In this function:

def foo(x=3):
    print(x)

You wouldn't expect a call of foo(27) to change the default value to
27, would you?

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


Thread

Default mutable parameters in functions fbicknel@gmail.com - 2014-04-03 11:49 -0700
  Re: Default mutable parameters in functions Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-03 15:32 -0600
  Re: Default mutable parameters in functions Ethan Furman <ethan@stoneleaf.us> - 2014-04-03 14:44 -0700
  Re: Default mutable parameters in functions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-04-03 20:27 -0400
  Re: Default mutable parameters in functions Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-04-04 01:38 +0100
  Re: Default mutable parameters in functions random832@fastmail.us - 2014-04-04 10:00 -0400
    Re: Default mutable parameters in functions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-04-04 14:34 +0000
      Re: Default mutable parameters in functions Chris Angelico <rosuav@gmail.com> - 2014-04-05 09:47 +1100
  Re: Default mutable parameters in functions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-04-04 19:23 -0400
    Re: Default mutable parameters in functions Roy Smith <roy@panix.com> - 2014-04-04 19:38 -0400
  Re: Default mutable parameters in functions Dave Angel <davea@davea.name> - 2014-04-04 22:21 -0400

csiph-web