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


Groups > comp.lang.python > #109366

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 09:38:38 -0400
Lines 103
Message-ID <mailman.93.1464874721.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> <mailman.75.1464841301.1839.python-list@python.org> <574fc214$0$1585$c3e8da3$5496439d@news.astraweb.com> <CA+FnnTxcVwkAuH2eUjYNCCEBCB54Rcz1SeE84MWqApD+pjB2Zw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de aGh3V9f5EMHNt6ebwJ0d4wtFPU5tRYEVJYrNcL7rCn6g==
Return-Path <ikorot01@gmail.com>
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; 'context': 0.05; 'subject:Question': 0.05; 'caller': 0.07; 'correct.': 0.07; 'cc:addr:python-list': 0.09; '(sorry': 0.09; '[1,': 0.09; 'lines:': 0.09; 'syntax': 0.13; 'def': 0.13; 'missed': 0.15; 'wed,': 0.15; '(but': 0.15; 'result.': 0.15; 'thu,': 0.15; '2016': 0.16; '[1].': 0.16; 'expects': 0.16; 'guys,': 0.16; 'igor': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'result:': 0.16; 'stepp': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; 'beginner': 0.18; 'steve': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'function,': 0.22; 'pass': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'point.': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'perl': 0.29; 'prints': 0.29; 'print': 0.30; 'url:mailman': 0.30; 'especially': 0.32; '[1]': 0.32; 'returned': 0.32; 'url:python': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'url:listinfo': 0.34; 'list': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'basic': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'someone': 0.38; 'thank': 0.38; 'hi,': 0.38; 'why': 0.39; 'does': 0.39; 'url:mail': 0.40; 'save': 0.60; 'back': 0.62; 'you.': 0.64; 'thursday': 0.66; 'here': 0.66; 'anywhere.': 0.84; 'compare:': 0.84
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=3cD/FDl1tD1ouv9IOHd5ZdJTP1oG9CW1Qof7PUrPKqU=; b=zASd2DWjOqH8YZKEuwQvtTNKsyC5OnDcoaFED2z2LxVz2Y95jfEISmdXCpBnPJLDb2 o2WvD1/dUPaAdLlkuzC3t+O0jdyPaxrdLxfZxPEizAwlI3hobN2AtgYBFB7R9KJ7A/p+ /f1HH9YjEotHfD6QBUdogyISHeBA23xjRTRhwBcJXwz6P2xK61TYCOfgG7mLeI33Lwk+ Dosj0fKTLbo1ddhHtqwJ+uy4EHPQ+cpXmwgAimqCwRi4kxLQNFHwf33Ej+ug/xhuWgfn K6XnCaALltPtUp3lWtdKHQ3aLhR8gjuKMSNr4p4Ool9pd+ErGyK5yXRYyqCTC2YclAz4 BtHg==
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=3cD/FDl1tD1ouv9IOHd5ZdJTP1oG9CW1Qof7PUrPKqU=; b=k3zPLA1DWEeX8/7mtorv0Kdt5Ho0vyLgzW0cZKyiNKf8OGJLQpYEktkF66W3rJuQFj sWY1LcAztfDFj6E9p5W4iqoo32yuKteilXD0GKb+UXHwUnnaCAC87sUxIP4awVhO6zxW 7I7+XGH9Ry61gi4zwejq0EDoCmRgBe24B6Trmu1512/9PkPFarkB1gkKkN3SydmWG2f6 0BmX7DgOw2paZcdrSsVNWk7WThwo2qmw941UMpIkXNQw2LQhH+itxa7duB1ILGfh7gY0 hwBxDIUQJo+aqlCvjtvrladlr6X6Zk98X+gf1+y+659/Tc3Ny6PCMpg4xcIIGKmREiRr FORw==
X-Gm-Message-State ALyK8tLd6Z2WrGITD6sDctGxHeZmyjzkHckX/n921Qg24W0OLqAxOjucGMc7qoem9bqfdJoI3hkDMJpdKTfhpg==
X-Received by 10.159.33.168 with SMTP id 37mr3486361uac.125.1464874718281; Thu, 02 Jun 2016 06:38:38 -0700 (PDT)
In-Reply-To <574fc214$0$1585$c3e8da3$5496439d@news.astraweb.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+FnnTxcVwkAuH2eUjYNCCEBCB54Rcz1SeE84MWqApD+pjB2Zw@mail.gmail.com>
X-Mailman-Original-References <ea789a85-a90c-4ff8-ab34-4b6dc1fc6c2f@googlegroups.com> <CANDiX9Jb2_eYhz0cUBJKcKz2Z7XEmTCXXedyMiAE7jpdnEd3GA@mail.gmail.com> <CA+FnnTwv80nimQY-FXDY8KrZ1gbMTqydGtSwBQt_6it8ztZCXg@mail.gmail.com> <mailman.75.1464841301.1839.python-list@python.org> <574fc214$0$1585$c3e8da3$5496439d@news.astraweb.com>
Xref csiph.com comp.lang.python:109366

Show key headers only | View raw


Steven,

On Thu, Jun 2, 2016 at 1:20 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Thursday 02 June 2016 14:21, Igor Korot wrote:
>
>> 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?
>
> Because he calls the function, then prints the return result.
>
> print(f(1))
>
> calls f(1), which returns [1], then prints [1].
>
> Then he calls:
>
> print(f(2))
>
> which returns [1, 2] (but he expects [2]), then prints it. And so on.
>
>
>> There is no line like:
>>
>> t = f(1)
>> print t
>
> Correct. But there are lines:
>
> print(f(1))
> print(f(2))
> print(f(3))

I think you missed the point.

Compare:

def f(a, L=[]):
     L.append(a)
     return L

print(f(1))
print(f(2))
print(f(3))

vs.

def f(a, L=[]):
     L.append(a)
     return L

t = f(1)
print t
t = f(2)
print t
t = f(3)
print t

For people that comes from C/C++/Java, the first syntax is kind of weird:
you return a value from the function but the caller does not save it anywhere.
Especially since the return is not a basic type and most of them are
not familiar
with scalar vs list context (sorry for the Perl terminology here)

Thank you.


>
>
>
> --
> Steve
>
> --
> 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