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; '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 To: python-list 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --=-1309113377-15397-23615-8053-1-= Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: quoted-printable 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=3D[]): > ... foo.append(1) > ... print foo > ... is def a(foo=3DNone): if foo is None: foo =3D [] foo.append(1) print foo -- = Corey Richardson "Those who deny freedom to others, deserve it not for themselves" -- Abraham Lincoln --=-1309113377-15397-23615-8053-1-= Content-Disposition: attachment; filename="signature.asc" Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.17 (GNU/Linux) iQEcBAEBCAAGBQJOB3wgAAoJEAFAbo/KNFvpOcYH/jrb1pD5t88KfyMDB4Uu/Rwc okXbnkppQaWv5yD6zGImVpQT07e8geHqfbWhVuu8EZFr4zwsXQPcLu8n4hTsltH6 KFpzdWEJnqdr1DkU26dJhKBGdLpaXil6WxqWA2dERPCvEYNetQnkjpxhLJMuSDq8 osfx9mMhLiNmM5AAcqi+8+lm7xhSmLTHBEgIT9GTnrpdbxEWOtDRGXdiEb0FQB6A 2yc1aMGSWJgcvBPKRCgU336pIe5/miN+e9UDiCHpc3RnKaodThS5QVJQCDI99BW4 czbWMPzQLaVMLDnYzpjJ7lJ5BibuB0SVrrXuLCzC7YpMa2/HpKXbp11k7AlOP5E= =m0c1 -----END PGP SIGNATURE----- --=-1309113377-15397-23615-8053-1-=--