Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Antoon Pardon Newsgroups: comp.lang.python Subject: Re: What does a list comprehension do Date: Thu, 26 Nov 2015 12:52:45 +0100 Lines: 34 Message-ID: References: <877fldnm9z.fsf@handshake.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de MZWca7vDRm8ruFNOlWgJegkGUNFGn/vXzLKvWr+it23A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:134': 0.05; 'subject:skip:c 10': 0.07; '(0,': 0.09; 'expression:': 0.09; 'python': 0.10; 'python.': 0.11; 'translate': 0.15; '(3,': 0.16; 'lambda': 0.16; 'received:ac.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'version.': 0.18; '>>>': 0.20; 'exceptions': 0.22; 'seems': 0.23; 'second': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'behaviour': 0.29; 'received:be': 0.30; 'surprised': 0.33; 'list': 0.34; 'gets': 0.35; 'could': 0.35; 'returning': 0.35; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'expect': 0.37; 'seem': 0.37; 'itself': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'worth': 0.67; 'act': 0.67; 'schreef': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Am8JAMnxVlaGuA9G/2dsb2JhbABehFcBwBuGDwKCCgEBAQEBAYVAAQEDASNmCxoCBSECAg8CRhMIAogiCK4HjBuEHAEBCAIhgQGFU4R+hSeCToFEBZZXjTWJEJNOY4QFhlEBAQE User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.8.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99563 Op 26-11-15 om 12:13 schreef Nobody: > Returning to the original expression: > > > q = [lambda x: i * x for i in range(4)] > > q[0](1), q[3](1) > (3, 3) > > q = [lambda x,i=i: i * x for i in range(4)] > > q[0](1), q[3](1) > (0, 3) Personnaly I would prefer: >>> q = [(lambda i: lambda x: i * x)(i) for i in range(4)] >>> q[0](1), q[3](1) (0, 3) And this is where I ask whether it would be worth the effort to change the behaviour of python. In general the following two seem equivallent: [ for x in ] and [(lambda x: )(x) for x in ] The only exceptions seems to be when is itself a lambda. It also seems that people who try this for the first time are surprised with what they get and seem to expect there list comprehension to act as if they had written the second version. So would it be advisable if python would translate the expression people write into a lambda that gets called? Are there issues that could come up?