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: 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; 'assign': 0.04; 'parameter': 0.05; 'python': 0.08; 'none:': 0.09; 'received:mail- bw0-f46.google.com': 0.09; 'subject:parameters': 0.09; 'wrote:': 0.15; 'executed.': 0.16; 'received:209.85.214.46': 0.16; 'cc:addr :python-list': 0.16; 'pm,': 0.16; 'def': 0.16; '(i.e.': 0.17; 'marc': 0.19; 'singh': 0.19; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'trying': 0.23; 'optional': 0.23; 'function': 0.26; "i'm": 0.27; 'received:209.85.214': 0.28; 'message-id:@mail.gmail.com': 0.28; 'checking': 0.28; 'discussed': 0.28; 'cc:addr:python.org': 0.30; 'sun,': 0.30; 'hi,': 0.30; 'subject:?': 0.31; 'values': 0.31; 'list': 0.33; 'does': 0.33; 'however,': 0.34; 'quite': 0.34; "isn't": 0.35; 'define': 0.35; 'url:python': 0.36; 'none': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.38; 'takes': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'list,': 0.39; 'received:209': 0.39; '26,': 0.67; 'jun': 0.67; '11:58': 0.84; '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; bh=cZnE0JnyPJF+BrKpGQS3L+4c2oTC7ZLwfEzci45GbOg=; b=Sf1/qLie5lmef4+03d/OQG/fbyAyPx/lTmlJemr8QwttD7xwHUyOMAV+x1SgFUNlPS +l2w+N5kGDjsO+1yRPKh8nqFhXY26wJJE0iRZBYMBL6orgbCOXQ/+CarErmM1QmzgKZL TYyPO5Hf/PLXTcmqY/H/rKi7uRTuIYLTftUOA= 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; b=hkjGAPKLIKS39nCVtSZaNaSCWVSoTzfMYQUlMwZTIlUx3G4X7vQOpQ4Od3SGgZH3ZD 7Kyinw4vHQ3hyJq747Iynr/0AceS06GmKpwVM53/ZOkJGVDGG1D0bUPueckno/T21H3W R2exszkXi1LQdTI9vwq9Z1USkisAZF8beMJ8w= 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: Shashank Singh Date: Mon, 27 Jun 2011 00:09:18 +0530 Subject: Re: Default value for optional parameters unexpected behaviour? To: Marc Aymerich Content-Type: text/plain; charset=ISO-8859-1 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 23 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1309113580 news.xs4all.nl 4354 [::ffff:82.94.164.166]:56138 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8476 On Sun, Jun 26, 2011 at 11:58 PM, Marc Aymerich 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)? This has been discussed before in this list, quite a few times http://mail.python.org/pipermail/python-list/2010-March/1239044.html A solution is to accept default value as None assign to [] by checking for None inside the function def f(a=None): if a is None: a = [] -- Regards Shashank Singh http://rationalpie.wordpress.com