Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.071 X-Spam-Evidence: '*H*': 0.86; '*S*': 0.00; 'tutorial': 0.03; 'modified': 0.07; '[1,': 0.09; 'def': 0.12; 'dictionary,': 0.16; 'mutable': 0.16; 'once.': 0.16; 'retained': 0.16; 'successive': 0.16; 'wrote:': 0.18; 'slightly': 0.19; 'print': 0.22; 'header :User-Agent:1': 0.23; '(or': 0.24; 'code:': 0.26; 'header:In- Reply-To:1': 0.27; 'function': 0.29; '[1]': 0.29; "i'm": 0.30; 'gary': 0.31; 'once,': 0.31; 'python.org': 0.32; 'classes': 0.35; 'created': 0.35; 'instances': 0.36; 'list': 0.37; 'received:10': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'most': 0.60; 'such': 0.63; 'more': 0.64; 'between': 0.67; 'default': 0.69; 'institute': 0.72; 'obvious': 0.74; 'received:10.10': 0.74; 'received:204': 0.75; 'dr.': 0.77; 'subject:Value': 0.84; 'why?': 0.91 Date: Wed, 19 Jun 2013 12:57:48 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Default Value References: <7e6361d5-6619-4aaa-adda-8b5f01bde57f@googlegroups.com> In-Reply-To: <7e6361d5-6619-4aaa-adda-8b5f01bde57f@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371672307 news.xs4all.nl 15882 [2001:888:2000:d::a6]:40667 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:48747 On 06/19/2013 12:17 PM, Ahmed Abdulshafy wrote: > I'm reading the Python.org tutorial right now, and I found this part rather strange and incomprehensible to me> > > Important warning: The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes This code: > def f(a, L=[]): > L.append(a) > return L does the same as this code: M=[] def f(a, L=M): L.append(a) return L where it's slightly more obvious that the list is created once, and modified with each call to the function (or rather with each call to the function that does not supply its own value for L). Gary Herron > > print(f(1)) > print(f(2)) > print(f(3)) > > This will print > [1] > [1, 2] > [1, 2, 3] > > How the list is retained between successive calls? And why? -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418