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


Groups > comp.lang.python > #82168

Re: Question on lambdas

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'subject:Question': 0.07; 'function,': 0.09; 'parameter': 0.09; 'creates': 0.14; '(note': 0.16; 'called,': 0.16; "didn't,": 0.16; 'expects': 0.16; 'expression,': 0.16; 'finney': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'function;': 0.16; 'helps.': 0.16; 'lambda': 0.16; 'lambda:': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'positional': 0.16; 'received:192.168.1.4': 0.16; 'sugar': 0.16; 'syntactic': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'examples': 0.20; 'starts': 0.20; 'creating': 0.23; 'header:User-Agent:1': 0.23; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'appreciated.': 0.29; "i'm": 0.30; 'changed.': 0.31; 'parameters.': 0.31; 'writes:': 0.31; 'allows': 0.31; 'trouble': 0.34; 'useful': 0.36; 'ben': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'skip:x 10': 0.40; 'expression': 0.60; 'hope': 0.61; 'new': 0.61; 'name': 0.63; 'different': 0.65; 'skip:\xe2 10': 0.65; '8bit%:40': 0.68; 'default': 0.69; '8bit%:43': 0.74; 'special': 0.74
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=ZuVjKrLG c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=xSRs65CQCjkA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=pGLkceISAAAA:8 a=mEsJUVyd_1CdlE8V5xEA:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett:2500
Date Tue, 09 Dec 2014 01:40:42 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Question on lambdas
References <m65d2m$3pe$1@ger.gmane.org> <85oarditil.fsf@benfinney.id.au>
In-Reply-To <85oarditil.fsf@benfinney.id.au>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.16752.1418089246.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418089246 news.xs4all.nl 2845 [2001:888:2000:d::a6]:57882
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82168

Show key headers only | View raw


On 2014-12-08 23:58, Ben Finney wrote:
> memilanuk <memilanuk@gmail.com> writes:
>
>> What I'm having trouble finding a concrete answer to is the difference
>> between:
>
> (Note that where you write “some_func” the syntax requires an
> expression, not a function. I've changed your examples to be clear).
>
>> lambda: some_expr
>
> This creates a new function which expects zero parameters. The function,
> when called, will return the value of ‘some_expr’.
>
>> lambda x: some_expr
>
> This creates a new function which expects one positional parameter named
> ‘x’. The function, when called, will return the value of ‘some_expr’.
>
>> lambda x=some_value: some_expr
>
> This creates a new function which expects one parameter named ‘x’, which
> parameter has a default value of ‘some_value’. The function, when
> called, will return the value of ‘some_expr’.
>
This is useful when you want to 'capture' the current value of
'some_value' to be passed to some_expr; if you didn't, then some_expr
would be using the value of 'some_value' at the time it was called,
which might be different if some variable it used had been changed.

>> Any help would be greatly appreciated.
>
> Hope that helps.
>
> It's best to remember that ‘lambda’ is syntactic sugar for creating a
> function; the things it creates are not special in any way, they are
> normal functions, not “lambdas”.
>
> What is different is that the function starts with no name bound to it,
> and the syntax allows only a single expression as the body of the
> function.
>

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


Thread

Re: Question on lambdas MRAB <python@mrabarnett.plus.com> - 2014-12-09 01:40 +0000

csiph-web