Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'python,': 0.01; 'javascript,': 0.05; 'way:': 0.05; 'python': 0.08; 'thoughts?': 0.09; 'to:name:python list': 0.09; '#the': 0.16; 'expression.': 0.16; 'lambda': 0.16; 'subject:function': 0.16; 'anonymous': 0.16; 'this:': 0.16; 'def': 0.16; 'functions,': 0.19; 'math': 0.21; 'suggests': 0.23; 'wonder': 0.23; 'statement': 0.25; 'function': 0.26; 'all,': 0.28; 'import': 0.29; 'it.': 0.33; 'to:addr:python- list': 0.34; 'skip:# 10': 0.34; 'define': 0.35; 'charset:us- ascii': 0.36; 'useful': 0.37; 'subject:with': 0.39; 'to:addr:python.org': 0.39; 'might': 0.39; 'mark': 0.40; 'received:98': 0.61; 'received:98.138.90': 0.65; 'received:bullet.mail.ne1.yahoo.com': 0.65; 'received:98.138': 0.67; 'received:mail.ne1.yahoo.com': 0.67; 'received:ne1.yahoo.com': 0.67 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 95393.317.bm@omp1002.mail.ne1.yahoo.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1310307703; bh=ej11xH1pWX7D2Qv36yEsPgbMK0hy8BYeeDBmRrQcI+4=; h=X-YMail-OSG:Received:X-Mailer:Message-ID:Date:From:Subject:To:MIME-Version:Content-Type; b=g/39pAYYy/y3nB0GxUCuz+X2qBg+/VEHaAfZicHGnjzvj4HOrySzCVdS/+QOB23wiBAtQIgA38t7GDfySOYDcfdYexaAw5Gvu5Y7RxqzQoxk3FOPupMU/7ENmCXz4pPYF0+7VaOLn0qJp95jAoxZiboq0odhsHTeUwGG4yz2Nlc= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Message-ID:Date:From:Subject:To:MIME-Version:Content-Type; b=vlnqO5VsJFgn1AhsABYQkJV1x3yFxL/Q+G1hgBnPM3yEyXMqMq7eT5J1V9gGuFoxY8pK8f7AYXc0oW5oJzqCNBFOfqymVHZzmL5xrTn3mJkkLZM7Mowbl/uusCPho7E0WrBs4gv+SCA0mFuJn5HXNRkWTqSRUL8Xh0W7NGKv5Mg=; X-YMail-OSG: wOtqAOUVM1nzVzM8Dyv9q7xg8xPhmooplGSkCwYQCsaVgHM 0qDPWnC9CTATooeStg3caQhVMd7sT3qxlU6vwHTq9tHstThttFBav.9ta40C fA5HON4loOEeoxseHJklo4gK9tL12QZiJpaAccKx_TTFrCqarCfiNXC1Z02M V875ovrvSDZ7sbWC_vTTxu1r.UevCdl.rnCO94cDPJUzs7R3Fwn0O3BnmzIW OJoF..AEOeBfH90gelhVSfFKUTOCkEE7nIw1YAgKb5N0lGF7mPjScoaFLJtC YjHg6TM2vpYdd8iO_thtfpZiveI25IMZ8kCGguwYzuVAwJBevBD9Krkc.wc5 s6t9_IPG6vd6TXFYudQhhKT5dIUj7cOqYywQ7jod5zmQJl7cPsKh.kg-- X-Mailer: YahooMailRC/572 YahooMailWebService/0.8.112.307740 Date: Sun, 10 Jul 2011 07:21:43 -0700 (PDT) From: Yingjie Lan Subject: anonymous function with multiple statements To: python list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1310307904 news.xs4all.nl 21759 [2001:888:2000:d::a6]:36259 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9164 Hi all, I wonder if Python provides a way to define anonymous functions containing multiple statements? With lambda form, we can only define a function of a single expression. In Javascript, it is possible to define a full-fledged anonymous functions, which suggests it is useful to have it. In Python, it might be like this: #============== def test( fn, K ): return sum(fn(i) for i in range(K))/K test( def (i): #indent from start of this line, not from 'def' from math import sin #the last statement ends with a ',' or ')'. return sin(i)+i*i;, 100) #';' to mark end of statement #another way: test( def (i): #indent from start of this line, not from 'def' from math import sin return sin(i)+i*i , 100) #freely place ',' anywhere #=============== Any thoughts? Yingjie