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


Groups > comp.lang.python > #82174

Re: Question on lambdas

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'example:': 0.03; 'talks': 0.03; 'parameters': 0.04; 'subject:Question': 0.07; '(self,': 0.09; 'function,': 0.09; 'guys.': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tia,': 0.09; 'python': 0.11; 'def': 0.12; 'creates': 0.14; "'a',": 0.16; '(at)': 0.16; 'bind': 0.16; 'buttons,': 0.16; 'called,': 0.16; 'called.': 0.16; 'expects': 0.16; 'finney': 0.16; 'lambda': 0.16; 'lambda:': 0.16; 'positional': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'passing': 0.19; 'example': 0.22; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'second': 0.26; 'defined': 0.27; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'went': 0.31; 'parameters.': 0.31; 'writes:': 0.31; 'anyone': 0.31; 'class': 0.32; 'supposed': 0.32; 'trouble': 0.34; 'skip:_ 10': 0.34; 'info': 0.35; 'classes': 0.35; 'something': 0.35; 'but': 0.35; 'instances': 0.36; 'method': 0.36; 'url:org': 0.36; 'two': 0.37; 'ben': 0.38; 'gmail': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; '(from': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'skip:u 10': 0.60; 'new': 0.61; 'first': 0.61; 'reach': 0.63; 'kind': 0.63; 'more': 0.64; 'skip:\xe2 10': 0.65; 'default': 0.69; 'url:htm': 0.73; '4th': 0.74; "it'd": 0.84; 'monte': 0.84; 'received:myvzw.com': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From memilanuk <memilanuk@gmail.com>
Subject Re: Question on lambdas
Date Mon, 08 Dec 2014 19:09:21 -0800
References <m65d2m$3pe$1@ger.gmane.org> <85oarditil.fsf@benfinney.id.au>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 62.sub-70-199-132.myvzw.com
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
In-Reply-To <85oarditil.fsf@benfinney.id.au>
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.16757.1418094575.18130.python-list@python.org> (permalink)
Lines 79
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418094575 news.xs4all.nl 2929 [2001:888:2000:d::a6]:47770
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82174

Show key headers only | View raw


On 12/08/2014 03:58 PM, Ben Finney wrote:
> memilanuk <memilanuk@gmail.com> writes:
>
>> What I'm having trouble finding a concrete answer to is the difference
>> between:

>
>> 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’.


so in the first example in my original post:

...
lambda: update_label2('A', 100)

would this work the same?  It looks as though it'd be passing the same 
two parameters to the same function...

lambda: 'A', 100: update_label2()


Also, in my second example:

class MyText(Text):
     def __init__(self, master, **kw):
         apply(Text.__init__, (self, master), kw)
         self.bind("<Return>", lambda e: "break")

(from the page @ 
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm, if anyone 
cares)

I'm kind of missing what 'e' is and where its defined and what it's 
supposed to be passing to "break"...?  The whole bind method for classes 
and instances is a wee bit above my pay grade right about now; but I 
happened to stumble upon this example when I went looking for more info 
on lambda :/




>
>> 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’.
>

I was reading in 'Programming Python 4th ed' by Lutz and he talks about 
something to do with default values vs. enclosing scopes...  that 
something like:

lambda x=x: some_expr

when evaluated inside a function loop to create buttons, etc., causes 
'x' to be evaluated as the default value at the function creation time, 
vs. when the function is actually called.  Do I have that more or less 
correct?

TIA,

Monte



-- 
Shiny!  Let's be bad guys.

Reach me @ memilanuk (at) gmail dot com

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


Thread

Re: Question on lambdas memilanuk <memilanuk@gmail.com> - 2014-12-08 19:09 -0800
  Re: Question on lambdas Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2014-12-09 11:46 +0100

csiph-web