Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'subject:Question': 0.07; 'function,': 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; 'creates': 0.14; '(note': 0.16; 'called,': 0.16; 'expects': 0.16; 'expression,': 0.16; 'finney': 0.16; 'function;': 0.16; 'helps.': 0.16; 'lambda': 0.16; 'lambda:': 0.16; 'positional': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sugar': 0.16; 'syntactic': 0.16; 'examples': 0.20; 'starts': 0.20; 'creating': 0.23; 'header:User-Agent:1': 0.23; "i've": 0.25; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'appreciated.': 0.29; "i'm": 0.30; 'parameters.': 0.31; 'writes:': 0.31; 'allows': 0.31; 'trouble': 0.34; 'effort': 0.37; 'ben': 0.38; 'to:addr:python-list': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; '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; 'worth': 0.66; '8bit%:40': 0.68; 'default': 0.69; '8bit%:43': 0.74; 'special': 0.74; 'received:125': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Question on lambdas Date: Tue, 09 Dec 2014 10:58:10 +1100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:lYQGqTmAHzJJDjf7IUQLM7TZVJQ= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418083098 news.xs4all.nl 2957 [2001:888:2000:d::a6]:60319 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82159 memilanuk 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’. > 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. -- \ “Theology is the effort to explain the unknowable in terms of | `\ the not worth knowing.” —Henry L. Mencken | _o__) | Ben Finney