Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'parameter': 0.05; 'definitions': 0.07; 'function,': 0.07; 'terry': 0.07; 'python': 0.08; '"default': 0.09; 'executed': 0.09; 'none):': 0.09; 'none:': 0.09; 'presume': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:parameters': 0.09; 'wrote:': 0.15; 'library': 0.15; '2:28': 0.16; 'chapter,': 0.16; 'executed.': 0.16; 'reedy': 0.16; 'type:': 0.16; 'pm,': 0.16; 'def': 0.16; '(i.e.': 0.17; 'marc': 0.19; 'jan': 0.19; 'language': 0.21; 'header:In-Reply-To:1': 0.22; 'trying': 0.23; 'optional': 0.23; 'reference.': 0.23; 'somewhere': 0.23; 'code': 0.24; 'says': 0.25; 'function': 0.26; 'fine': 0.26; "i'm": 0.27; 'definition': 0.30; 'hi,': 0.30; 'subject:?': 0.31; 'values': 0.31; 'list': 0.33; 'does': 0.33; 'header:User-Agent:1': 0.34; 'header:X -Complaints-To:1': 0.34; 'however,': 0.34; 'to:addr:python-list': 0.34; '...': 0.34; "isn't": 0.35; 'define': 0.35; 'received:org': 0.38; 'takes': 0.38; 'subject:: ': 0.38; 'put': 0.38; 'should': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'body': 0.63; 'our': 0.63; 'subject:value': 0.84; 'bold': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Default value for optional parameters unexpected behaviour? Date: Sun, 26 Jun 2011 14:46:23 -0400 References: <2a25a54a-c60b-4811-9c6a-97c7f717dccb@hd10g2000vbb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.18) Gecko/20110616 Lightning/1.0b2 Thunderbird/3.1.11 In-Reply-To: <2a25a54a-c60b-4811-9c6a-97c7f717dccb@hd10g2000vbb.googlegroups.com> 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: 25 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1309113996 news.xs4all.nl 4371 [::ffff:82.94.164.166]:53814 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8480 On 6/26/2011 2:28 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)? Our fine Language Reference. Compound Statements chapter, Function definitions section, says in bold type: "Default parameter values are evaluated when the function definition is executed. ". I presume the tutorial says this somewhere too. Read both, along with the first 5 chanpter of the Library reference. If you want code executed when you call the function, put it in the body that is executed when you call the function def f(lst = None): if lst is None: lst = [] ... -- Terry Jan Reedy