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


Groups > comp.lang.python > #69624

Re: Default mutable parameters in functions

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; '%s"': 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; '[3],': 0.16; 'behave': 0.16; 'expecting': 0.16; 'foo()': 0.16; 'message-id:@4ax.com': 0.16; 'picks': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:parameters': 0.16; 'thu,': 0.19; '>>>': 0.22; '(in': 0.22; 'print': 0.22; 'url:home': 0.24; 'regardless': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; "i'm": 0.30; '(since': 0.31; 'created': 0.35; 'no,': 0.35; 'charset:us-ascii': 0.36; 'list': 0.37; 'list.': 0.37; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'email addr:gmail.com': 0.63; 'different': 0.65; 'default': 0.69; 'acts': 0.74; '(imho).': 0.84; "don't...": 0.84; 'received:108': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Default mutable parameters in functions
Date Thu, 03 Apr 2014 20:27:59 -0400
Organization IISS Elusive Unicorn
References <ac388f0b-8951-42af-a94f-33abdb7231e7@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host adsl-108-79-218-194.dsl.klmzmi.sbcglobal.net
X-Newsreader Forte Agent 6.00/32.1186
X-No-Archive YES
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.8869.1396571282.18130.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396571282 news.xs4all.nl 2895 [2001:888:2000:d::a6]:45074
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69624

Show key headers only | View raw


On Thu, 3 Apr 2014 11:49:56 -0700 (PDT), fbicknel@gmail.com declaimed the
following:

>But mutables behave very strangely (imho).  Take this example:
>
	No, they don't...


	<snip>

>it picks up where it left off.
>
	As it should...

>I was rather expecting it to start with 4!

>>> def foo(bar = [42]):
... 	print "I'm %s with %s" % (id(bar), bar[0])
... 	bar[0] += 1
... 	
>>> foo()
I'm 70238984 with 42
>>> foo()
I'm 70238984 with 43
>>> foo()
I'm 70238984 with 44
>>> foo([3])
I'm 70242184 with 3
>>> foo([3])
I'm 70668424 with 3
>>> foo()
I'm 70238984 with 45
>>> 

	The default was evaluated once -- it is a list with ID 70238984 (in
this case), and that ID will not change regardless of what goes on inside
the list.

	When called with [3], the function acts on a different list created
just for that call (since it was a list literal).

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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