Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: boB Stepp Newsgroups: comp.lang.python Subject: Re: Beginner Question Date: Wed, 1 Jun 2016 20:42:34 -0500 Lines: 36 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 6WapvBpKALW+5h+ffYpnRA4WEvZZfJW4EZdmaxIuN4zw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:Question': 0.05; 'python': 0.10; 'def': 0.13; 'wed,': 0.15; 'argument': 0.15; '2016': 0.16; 'assigns': 0.16; 'from:addr:robertvstepp': 0.16; 'from:name:bob stepp': 0.16; 'hits.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'result:': 0.16; 'wrote:': 0.16; 'beginner': 0.18; 'pass': 0.22; 'header:In-Reply-To:1': 0.24; '(which': 0.26; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'consequence': 0.29; 'url:category': 0.29; '[1]': 0.32; 'returned': 0.32; 'lists': 0.34; 'list': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:209': 0.38; 'someone': 0.38; 'google': 0.39; 'why': 0.39; 'received:209.85.214': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'behavior': 0.61; 'default': 0.61; 'back': 0.62; 'fact,': 0.67; 'url:wordpress': 0.79 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to; bh=dgV0nNQZ7aD+W6wS30C0fHGTFkOl3HNg5xqyDcn5RWk=; b=ZiszwdF9OVQEDxv4WJDaYREXrPYeW62MJfx99RAd/0kHcsoLi6ApJPWzmF3r8aNmZp H5YSkxeybmaKMlpG6gGkL4tzkMQv5hJG6+xnlKa+KTnc1D1MaqqF5pcgS+8hACLvARHd F/8uDUlHN27waYtEqUl0Z6qHyN7EdksNjmmBmwAoJSidJpVgKZzE44qes6DPo306E4DW qcot6HOSXzQbABaCNjD0ZIcUqB5Oz4EqIICJmJhgelNB3vL+amIcDkio9bGluVsgvr89 kybo8qfMoc9Al8veVIue6yxpTCksLN8UOwUEAlFhEIFYsG4oIm55l2kF8r1OMUK6K5HE R9EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=dgV0nNQZ7aD+W6wS30C0fHGTFkOl3HNg5xqyDcn5RWk=; b=LJiqwENSt4aU/5eeC4wwzInngbmGIca5nXkw+sKL8GJKvAhYpIHEbr+9Vso04I5tfs 2jWzeVrz8DZK6TE2vB95SwrG+2C5IMBCJrDVIVNt0EZ2sYvzEJRg6zTrBqUjlJSt6+E9 /W6YONpFnofPELal7TPwq7IUa+J7NZVAKwyLUfpPJaZnY12Vm2eKTuMzqQi+FJsNHM7L APg4vsSMqTFfB1nrk2LLlPpC0kc4yyVuyY906EM4ZQLGAiGlz7m/TYVB3oqAGAQcMi0a QY3Iy2K+eAOW1qKIjNIG+csrbl3YfPiC54v1Sll5G2IAdGDrMTxqJc3NTmHyIM+aMUmw vKgw== X-Gm-Message-State: ALyK8tK8MG81p/Q2U56T+G/E67NP2G5TdjlslBKtzW0KJVVLEcY9sdjYkaJUbYVswVWGWO8IATcXfeXksjh/bQ== X-Received: by 10.36.55.138 with SMTP id r132mr877695itr.73.1464831754901; Wed, 01 Jun 2016 18:42:34 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:109326 On Wed, Jun 1, 2016 at 7:55 PM, Marcin Rak wrote: > Hi to all > > I have a beginner question to which I have not found an answer I was able to understand. Could someone explain why the following program: > > def f(a, L=[]): > L.append(a) > return L > > print(f(1)) > print(f(2)) > print(f(3)) > > gives us the following result: > > [1] > [1,2] > [1,2,3] > > How can this be, if we never catch the returned L when we call it, and we never pass it on back to f??? This comes up rather frequently. In fact, if you just copy your function (Which is used in the official Python tutuorial.) and paste it into Google you will get some relevant hits. One such is: https://pythonconquerstheuniverse.wordpress.com/category/python-gotchas/ As the link will explain the behavior you observe is a consequence of two things: When Python assigns the default argument for the empty list and that lists are *mutable*. Enjoy! -- boB