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


Groups > comp.lang.python > #8478

Re: Default value for optional parameters unexpected behaviour?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <kb1pkl@aim.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'parameter': 0.05; 'function,': 0.07; 'python': 0.08; 'content- type:multipart/signed': 0.09; 'filename:fname piece:signature': 0.09; 'foo': 0.09; 'header:In-reply-to:1': 0.09; 'none:': 0.09; 'subject:parameters': 0.09; 'abraham': 0.16; 'content- type:application/pgp-signature': 0.16; 'evaluates': 0.16; 'executed.': 0.16; 'filename:fname piece:asc': 0.16; 'filename:fname:signature.asc': 0.16; 'from:addr:kb1pkl': 0.16; 'from:name:corey richardson': 0.16; 'richardson': 0.16; 'sees': 0.16; '>>>': 0.16; 'def': 0.16; '(i.e.': 0.17; 'marc': 0.19; 'to:name:python-list': 0.19; 'trying': 0.23; 'optional': 0.23; 'function': 0.26; "i'm": 0.27; 'keeps': 0.28; 'around.': 0.29; 'object': 0.30; '-0400': 0.30; 'thanks': 0.30; 'hi,': 0.30; 'subject:?': 0.31; 'values': 0.31; 'print': 0.32; 'list': 0.33; 'does': 0.33; 'header:User-Agent:1': 0.34; 'instead': 0.34; 'however,': 0.34; 'to:addr:python-list': 0.34; '...': 0.34; 'received:71': 0.34; "isn't": 0.35; 'define': 0.35; 'takes': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'common': 0.39; 'to:addr:python.org': 0.39; 'header:Message-Id:1': 0.61; 'received:172.29': 0.64; 'received:r1000.mx.aol.com': 0.64; 'from:addr:aim.com': 0.67; 'jun': 0.67; 'freedom': 0.68; '2011:': 0.84; 'idiom': 0.84; 'received:172.29.41.70': 0.84; 'received :mtaout-mb06.r1000.mx.aol.com': 0.84; 'subject:value': 0.84; 'received:172.29.41': 0.91
Subject Re: Default value for optional parameters unexpected behaviour?
From Corey Richardson <kb1pkl@aim.com>
To python-list <python-list@python.org>
In-reply-to <2a25a54a-c60b-4811-9c6a-97c7f717dccb@hd10g2000vbb.googlegroups.com>
References <2a25a54a-c60b-4811-9c6a-97c7f717dccb@hd10g2000vbb.googlegroups.com>
Date Sun, 26 Jun 2011 14:36:12 -0400
User-Agent Sup/0.12.1
Content-Transfer-Encoding 8bit
MIME-Version 1.0
Content-Type multipart/signed; micalg="pgp-sha256"; boundary="=-1309113377-15397-23615-8053-1-="; protocol="application/pgp-signature"
x-aol-global-disposition G
X-AOL-VSS-INFO 5400.1158/71779
X-AOL-VSS-CODE scan_error
X-AOL-SCOLL-SCORE 0:2:445436544:93952408
X-AOL-SCOLL-URL_COUNT 0
x-aol-sid 3039ac1d29464e077c2c328b
X-AOL-IP 71.181.74.141
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.426.1309113702.1164.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 82.94.164.166
X-Trace 1309113703 news.xs4all.nl 4366 [::ffff:82.94.164.166]:56542
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8478

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Excerpts from Marc Aymerich's message of Sun Jun 26 14:28:30 -0400 2011:
> 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 :)
> 

Really common mistake, I made it myself too. When Python evaluates the 
function, it sees the default parameter of `foo' as the new object you
create with []. It keeps that object around. The proper idiom instead of

> >>> def a(foo=[]):
> ...  foo.append(1)
> ...  print foo
> ...

is

def a(foo=None):
	if foo is None:
		foo = []
	foo.append(1)
	print foo
-- 
Corey Richardson
  "Those who deny freedom to others, deserve it not for themselves"
     -- Abraham Lincoln

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


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