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


Groups > comp.lang.python > #109332

Re: Beginner Question

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Igor Korot <ikorot01@gmail.com>
Newsgroups comp.lang.python
Subject Re: Beginner Question
Date Thu, 2 Jun 2016 00:21:33 -0400
Lines 55
Message-ID <mailman.75.1464841301.1839.python-list@python.org> (permalink)
References <ea789a85-a90c-4ff8-ab34-4b6dc1fc6c2f@googlegroups.com> <CANDiX9Jb2_eYhz0cUBJKcKz2Z7XEmTCXXedyMiAE7jpdnEd3GA@mail.gmail.com> <CA+FnnTwv80nimQY-FXDY8KrZ1gbMTqydGtSwBQt_6it8ztZCXg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de n2Ra0SiD6oC3LCznGemC9AwXHMG9ZFi/0blG4xgmt1cg==
Return-Path <ikorot01@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:Question': 0.05; 'list?': 0.07; 'cc:addr:python-list': 0.09; 'thrown': 0.09; 'python': 0.10; 'def': 0.13; 'wed,': 0.15; 'argument': 0.15; '2016': 0.16; 'assigns': 0.16; 'guys,': 0.16; 'hits.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'result:': 0.16; 'stepp': 0.16; 'wrote:': 0.16; 'beginner': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '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; 'print': 0.30; 'url:mailman': 0.30; '[1]': 0.32; 'returned': 0.32; 'url:python': 0.33; 'url:listinfo': 0.34; 'lists': 0.34; 'list': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'so,': 0.35; 'could': 0.35; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'someone': 0.38; 'thank': 0.38; 'hi,': 0.38; 'google': 0.39; 'why': 0.39; 'does': 0.39; 'rather': 0.39; 'url:mail': 0.40; 'some': 0.40; 'your': 0.60; 'behavior': 0.61; 'default': 0.61; 'back': 0.62; 'you.': 0.64; 'here': 0.66; '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 :cc; bh=7Nt3l4MzOB9CnRg+WOWqMQN1rdi6M0bSj/bfbbWnP7k=; b=cmEPUNj61po6bPdJZk1WztyzXl8Y8FuYQkcLfkzvacxuuy+3Jj1LP3zvjaKF4D6aCA lJLqq0+w0tYW6QUKzsV0JkesijwiNhkZImR0pvV9T6mh+JgJgAYdVGRV9T2UMzsgs1Qm Jc1wtXgCLR9RDwLJ0CBWaS0IPvjQRVTkI9xFNlzFc+60HDsF1lyzZ6zV0hN1bo8I/tD9 7+uqcf7sPkovC7MBnBCMJtyCfj3yRVMSrX0wU71/PMBDjpxGWR+KhkYwESiDU+UiVFEV eWYdCc/gB1qWP+PxXqDXu88h/JYGLDCR6eZFP/Ajv/6HLwLO6AoblkwVwmHdz56lVaKg feEg==
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:cc; bh=7Nt3l4MzOB9CnRg+WOWqMQN1rdi6M0bSj/bfbbWnP7k=; b=aSbGi4PxC1Fl3I4fRLOoTnLHah3VB5lxXCeKIU1aGDK5QiYf4uvOAFLL0B4d8dY+kT p1dmC8xRBA4/8HltmDUPmExbsa+8eOUG+XW//YKboAvYUbOc7eBLQTBMjdIhkcU0EJTk TH0OdKX9auRdBhYnIdzkyGX5rJnrNh58SYFuipOiJ3KkyporVEKwCBmp1y0ss1fRa59R j1NPNtCbMVVZD9cJnJHo4l3MmibSHslF9BMKNW/siYgTIp/zRKt++d40dGWyMPshcr16 3Yh/hVzg05uNI8drAXtUT9MbzD3P3yWLWQGUskuxdSkzUZjszk6L6s3oxcLusR0mll8m +3TQ==
X-Gm-Message-State ALyK8tLhZctOJTKywHlbHdVUHeRjNnQiZ5GPj28bCMCMQdJW0vuf0KTJa4PCEQs/B1uTVSyaM4CUgXJm2tRaTQ==
X-Received by 10.31.49.78 with SMTP id x75mr3638554vkx.158.1464841293354; Wed, 01 Jun 2016 21:21:33 -0700 (PDT)
In-Reply-To <CANDiX9Jb2_eYhz0cUBJKcKz2Z7XEmTCXXedyMiAE7jpdnEd3GA@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CA+FnnTwv80nimQY-FXDY8KrZ1gbMTqydGtSwBQt_6it8ztZCXg@mail.gmail.com>
X-Mailman-Original-References <ea789a85-a90c-4ff8-ab34-4b6dc1fc6c2f@googlegroups.com> <CANDiX9Jb2_eYhz0cUBJKcKz2Z7XEmTCXXedyMiAE7jpdnEd3GA@mail.gmail.com>
Xref csiph.com comp.lang.python:109332

Show key headers only | View raw


Hi, guys,

On Wed, Jun 1, 2016 at 9:42 PM, boB Stepp <robertvstepp@gmail.com> wrote:
> On Wed, Jun 1, 2016 at 7:55 PM, Marcin Rak <mrak@sightlineinnovation.com> 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???

I think the OP question here is:

Why it is printing the array?
There is no line like:

t = f(1)
print t

So, why the first print does print the list? The return value should
be thrown away...

Thank you.

>
> 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
> --
> https://mail.python.org/mailman/listinfo/python-list

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Beginner Question Marcin Rak <mrak@sightlineinnovation.com> - 2016-06-01 17:55 -0700
  Re: Beginner Question boB Stepp <robertvstepp@gmail.com> - 2016-06-01 20:42 -0500
    Re: Beginner Question Marcin Rak <mrak@sightlineinnovation.com> - 2016-06-02 06:10 -0700
  Re: Beginner Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-02 13:53 +1000
  Re: Beginner Question Igor Korot <ikorot01@gmail.com> - 2016-06-02 00:21 -0400
    Re: Beginner Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-02 15:20 +1000
      Re: Beginner Question Igor Korot <ikorot01@gmail.com> - 2016-06-02 09:38 -0400
        Re: Beginner Question sohcahtoa82@gmail.com - 2016-06-02 11:02 -0700

csiph-web