Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: What is a function parameter =[] for? Date: Wed, 18 Nov 2015 15:47:00 -0700 Lines: 56 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de /nd+jIz3TmdbQ9jB3U2/wQaTipppRCD2NajfjZC7Gvvg== 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; 'received:209.85.223': 0.03; 'value,': 0.03; 'none:': 0.05; '[],': 0.07; 'calls.': 0.07; 'wednesday,': 0.07; 'defined,': 0.09; 'mutable': 0.09; 'none.': 0.09; 'retained': 0.09; 'example:': 0.10; 'python.': 0.11; 'def': 0.13; 'wed,': 0.15; 'value.': 0.15; 'called,': 0.16; 'effect,': 0.16; 'list1': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'values:': 0.16; 'wrote:': 0.16; 'thanks.': 0.18; 'changes': 0.20; '2015': 0.20; 'parameter': 0.22; 'thanks,': 0.24; 'second': 0.24; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'initially': 0.30; 'usually': 0.33; 'me?': 0.34; 'previous': 0.34; 'list': 0.34; 'advice': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'nov': 0.35; 'something': 0.35; 'but': 0.36; 'list,': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'setting': 0.37; 'list.': 0.37; 'received:209': 0.38; 'hi,': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'avoid': 0.61; 'default': 0.61; 'between': 0.65; 'therefore': 0.67; 'consequently': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=9BxU8liI5gFEjv/fiVqhLVK/oO/jqD5nxU6uK0PrDzM=; b=mpwWUC0NMxFnPl8FDYJXeULRqe1JALnVjzxsJ+AI+MOOR0NqMKIqye5ZQmViaBskzv ixxXB1OrpRw36pO2fJZh14SdlHUZSGmFyp89o5ZvB9MgJiXMBLkNc3QEkjlWLDiG8IYj IG+6eGgxsYQqEYFlalszy6j226GQxDnE+8a1J7gE3nnEm2ZZAiO6WzyiOwxpMfUkk2GR ofZldiiMyExpbLn5FUfCGNHbcLLS5lJ/KmpoDRaGQUZSLBj8zaurBgVnMLkE/67IHvQt GAuLXaoezzEUzLgEkdQ4zJhgJ5t5gsMzREm3BjFhLyIWxKw4mtgYxguFqw/L+9pM6a+I p3hw== X-Received: by 10.107.137.226 with SMTP id t95mr5427375ioi.188.1447886859442; Wed, 18 Nov 2015 14:47:39 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99008 On Wed, Nov 18, 2015 at 3:38 PM, fl wrote: > On Wednesday, November 18, 2015 at 5:12:44 PM UTC-5, Ian wrote: >> On Wed, Nov 18, 2015 at 2:08 PM, fl wrote: >> > Hi, >> > >> > I have tried the below function and find that it can remember the previous >> > setting value to 'val'. I think the second parameter has something on this >> > effect, but I don't know the name and function of '=[]' in this application. >> > >> > Could you explain a little to me? >> > Thanks, >> > >> > >> > def eList(val, list0=[]): >> > list0.append(val) >> > return list0 >> > list1 = eList(12) >> > list1 = eList('a') >> >> The list0 parameter has a default value, which is [], an initially >> empty list. The default value is evaluated when the function is >> defined, not when it is called, so the same list object is used each >> time and changes to the list are consequently retained between calls. > > Thanks. The amazing thing to me is that the following two line codes: > list1 = eList(12) > list2 = eList('a') > > will have both list1 and list2 the same cascaded values: > > list1 > Out[2]: [12, 'a'] > > list2 > Out[3]: [12, 'a'] > > I have known object concept in Python. > 1. Why do they have the same list value? > Function eList must be for this purpose? Because eList returns list0, and as explained above list0 is the same list in both calls. Therefore list1 and list2 are the same list. > 2. If I want to have two separate lists, how to avoid the above result? > Function eList is not for this purpose? The usual advice is to not use a mutable object as the default value. If you want the default to be an empty list, it's usually better set it to None and then make it an empty list if the value is None. For example: def eList(val, list0=None): if list0 is None: list0 = [] list0.append(val) return list0