Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Antoon Pardon Newsgroups: comp.lang.python Subject: What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?)) Date: Wed, 25 Nov 2015 14:51:23 +0100 Lines: 64 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 hxCPSIaFKh4doLiE8prKhAv/UgQRrj3JHrmN3q6byumA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'essentially': 0.04; 'received:134': 0.05; 'subject:skip:c 10': 0.07; 'def': 0.13; 'discussions': 0.15; 'subject: \n ': 0.15; 'iterated': 0.16; 'lambda': 0.16; 'python3.': 0.16; 'received:ac.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'of.': 0.18; 'variable': 0.18; '(or': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'subject:list': 0.26; 'addition,': 0.27; 'function': 0.28; 'received:be': 0.30; 'especially': 0.32; 'changing': 0.34; 'list': 0.34; 'gives': 0.35; 'important.': 0.35; 'something': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'expect': 0.37; 'missing': 0.37; 'difference': 0.38; 'late': 0.38; 'someone': 0.38; 'why': 0.39; 'does': 0.39; 'subject:-': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'more': 0.63; 'binding': 0.66; 'of?': 0.84; 'schreef': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnkKAC28VVaGuA9G/2dsb2JhbABehFcBwFqGDwKCDQEBAQEBAYVAAQEEI1URJQIFFgQHAgIJAwIBAgFFEwgCiCqtTowQhCUBAQgCIYEBhVOKJYJOgUQFlleBA4wyiRCTTmOCRIFBhh0BAQE User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.8.0 In-Reply-To: <877fldnm9z.fsf@handshake.de> 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:99452 Op 20-11-15 om 08:49 schreef dieter: > In addition, the last few days have had two discussions in this list > demonstrating the conceptial difficulties of late binding -- one of them: > > Why does "[lambda x: i * x for i in range(4)]" gives > a list of essentially the same functions? Can you (or someone else) explain what a list comprehension is equivallent of. Especially in python3. Take this simple list comprhesion: [x * x for x in range(10)] what would this be equivallent of? Something like: def lch1(): ls = [] for x in range(10): ls.append(x * x) return ls Or more something like: def lch2(): def expr(x): return x * x ls = [] for x in range(10): ls.append(expr(x)) return ls For this example it doesn't make a difference but for the example above it would become important. def lch3(): ls = [] for i in range(4): ls.append(lambda x: i * x) return ls versus def lch4(): def expr(i): return lambda x: i * x ls = [] for i in range(4) ls.append(expr(i)) return ls Now from the result we get I expect list comprehensions to work more like lch1 and lch3 rather than lch2 and lch4. But I can understand people who think of the expression as a function of the variable that is iterated over. Am I missing something? Would it be worthwile considering changing this behaviour? -- Antoon.