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


Groups > comp.lang.python > #8480

Re: Default value for optional parameters unexpected behaviour?

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 <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.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 <tjreedy@udel.edu>
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 <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.428.1309113996.1164.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

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