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


Groups > comp.lang.python > #8481

Re: Default value for optional parameters unexpected behaviour?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <bsk16@case.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'arguments': 0.05; 'parameter': 0.05; 'python': 0.08; 'foo': 0.09; 'nameerror:': 0.09; 'object.': 0.09; 'subject:parameters': 0.09; 'am,': 0.13; 'defined': 0.14; 'wrote:': 0.15; '[])': 0.16; 'executed.': 0.16; 'object,': 0.16; '>>>': 0.16; 'def': 0.16; '(i.e.': 0.17; 'marc': 0.19; '(most': 0.21; 'header:In-Reply-To:1': 0.22; 'trying': 0.23; 'optional': 0.23; 'traceback': 0.25; 'statement': 0.25; 'function': 0.26; "i'm": 0.27; 'message-id:@mail.gmail.com': 0.28; 'sun,': 0.30; 'thanks': 0.30; 'hi,': 0.30; 'subject:?': 0.31; 'values': 0.31; 'list': 0.33; 'does': 0.33; 'actually': 0.33; 'received:209.85.160': 0.34; 'however,': 0.34; 'to:addr:python- list': 0.34; 'creates': 0.34; 'executable': 0.35; 'last):': 0.35; "isn't": 0.35; 'define': 0.35; 'file': 0.36; 'functions.': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'takes': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.39; '26,': 0.67; 'jun': 0.67; "'foo'": 0.84; 'subject:value': 0.84
MIME-Version 1.0
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 12:21:55 -0700
Subject Re: Default value for optional parameters unexpected behaviour?
From Benjamin Kaplan <benjamin.kaplan@case.edu>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-Junkmail-Status score=10/49, host=mpv1.tis.cwru.edu
X-Junkmail-Signature-Raw score=unknown, refid=str=0001.0A020206.4E0786D4.008E,ss=1,fgs=0, ip=209.85.160.54, so=2010-12-23 16:51:53, dmn=2009-09-10 00:05:08, mode=single engine
X-Junkmail-IWF false
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.430.1309116126.1164.python-list@python.org> (permalink)
Lines 29
NNTP-Posting-Host 82.94.164.166
X-Trace 1309116126 news.xs4all.nl 4361 [::ffff:82.94.164.166]:33523
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8481

Show key headers only | View raw


On Sun, Jun 26, 2011 at 11:28 AM, Marc Aymerich <glicerinu@gmail.com> 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)?
>
> Thanks :)
>

So the thing about Python is that you don't actually declare
functions. You create them. def is an executable statement that
creates a function object. Default arguments are part of the function
object, so they get evaluated when the function is created.

>>> foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> def foo(a = []) :
...     a.append(1)
...
>>> foo.func_defaults
([],)
>>> foo()
>>> foo.func_defaults
([1],)
>>>

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