Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'interpreter': 0.04; '(even': 0.05; 'cache': 0.05; 'case.': 0.05; 'context': 0.05; 'correct.': 0.07; 'differently': 0.07; 'indexing': 0.07; 'indices': 0.07; 'parameter': 0.07; 'repeated': 0.07; 'python': 0.09; '(instead': 0.09; 'broken.': 0.09; 'cached': 0.09; 'parsed': 0.09; 'semantics': 0.09; 'thread,': 0.09; 'stored': 0.10; 'files.': 0.13; 'index': 0.13; "(i'm": 0.16; '(people': 0.16; 'bracket': 0.16; 'caches': 0.16; 'caching': 0.16; 'comma': 0.16; 'constructs': 0.16; 'devs': 0.16; 'fond': 0.16; 'fyi,': 0.16; 'integer.': 0.16; 'interpreter;': 0.16; 'invokes': 0.16; 'keywords.': 0.16; 'lambda': 0.16; 'list;': 0.16; 'sequence.': 0.16; 'slice.': 0.16; 'slicing,': 0.16; 'xrange': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'creates': 0.18; 'saying': 0.18; 'variable': 0.20; 'equivalent': 0.20; 'respective': 0.20; 'suggested': 0.20; 'meant': 0.21; 'latter': 0.22; 'operations.': 0.22; 'subject:skip:i 10': 0.22; 'needed.': 0.23; 'specified': 0.23; 'raise': 0.24; 'idea': 0.24; 'second': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'common': 0.26; 'values': 0.26; 'andrew': 0.27; 'execution': 0.27; 'instead.': 0.27; 'object,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:( 20': 0.28; 'subject:list': 0.28; 'once,': 0.29; 'reduced': 0.29; 'probably': 0.29; "i'm": 0.29; 'related': 0.30; 'keyword': 0.30; 'received:209.85.215.46': 0.30; 'basic': 0.30; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; 'function.': 0.33; 'traditional': 0.33; 'utility': 0.33; 'to:addr :python-list': 0.33; 'times.': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'done': 0.34; 'list': 0.35; 'nov': 0.35; 'sequence': 0.35; 'so,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'list.': 0.35; 'created': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'method': 0.36; 'anything': 0.36; 'ok,': 0.37; 'uses': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'range': 0.60; 'map': 0.61; 'first': 0.61; 'time,': 0.62; 'evaluate': 0.62; 'different': 0.63; 'times': 0.63; 'taking': 0.65; 'frequently': 0.65; 'contact': 0.68; 'construction': 0.72; 'special': 0.73; 'case?': 0.84; 'complaint': 0.84; 'ini': 0.84; 'start.': 0.84; 'stop,': 0.84; 'to:name:python': 0.84; 'url:functions': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=cJmyBulgYGpXclgJwPImVwDvmRuKiaQg1kSoj8x0ozM=; b=NfwHchx1mpwjIMZRe/XyCREMwu5UJKcrT00IdEuJZDpAIDm+bfqnOqGkChTnY6AUkT m2VlSmBEvBO91uHcPfYUooaeky4gNP+9p1hjbLEiQlmdrikIXqj3gdeESsRMWkVKq2Hr vLSzMBY1VrAl/w5JoT/K+BiWAsTUrcPCYXDiSNVzUiL5Nc3fLNCr8FOcdzDDHCDdOYAZ we4U7RAhzEDI8PSYhE41OmHsHg24YSoT/nXjeZW68Kvy1+cr8t7zrSKaocLpsE7lppEy KSdVo6Sr8LMw9bL7H3PU+CyRRwKm+SFZav7VpROzCf/kTil1aexdPrvtALqK2c4CYwiN 7xbQ== MIME-Version: 1.0 In-Reply-To: <509B2355.60407@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ADA6E.10403@r3dsolutions.com> <509B2355.60407@r3dsolutions.com> From: Ian Kelly Date: Thu, 8 Nov 2012 00:09:35 -0700 Subject: Re: Multi-dimensional list initialization To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 97 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352358609 news.xs4all.nl 6873 [2001:888:2000:d::a6]:39753 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32933 On Wed, Nov 7, 2012 at 8:13 PM, Andrew Robinson wrote: > OK, and is this a main use case? (I'm not saying it isn't I'm asking.) I have no idea what is a "main" use case. > There is a special keyword which signals the new type of comprehension; A > normal comprehension would say eg: '[ foo for i in xrange ]'; but when the > 'for i in' is reduced to a specific keyword such as 'ini' (instead of > problematic 'in') the caching form of list comprehension would start. FYI, the Python devs are not very fond of adding new keywords. Any time a new keyword is added, existing code that uses that word as a name is broken. 'ini' is particularly bad, because 1) it's not a word, and 2) it's the name of a common type of configuration file and is probably frequently used as a variable name in relation to such files. > So, then, just like a comprehension -- the interpreter will begin to > evaluate the code from the opening bracket '['; But anything other than a > function/method will raise a type error (people might want to change that, > but it's safe). > > The interpreter then caches all functions/initialiser methods it comes into > contact with. > Since every function/method has a parameter list (even if empty); The > interpreter would evaluate the parameter list on the first pass through the > comprehension, and cache each parameter list with it's respective function. > > When the 'ini' keyword is parsed a second time, Python would then evaluate > each cached function on its cached parameter list; and the result would be > stored in the created list. > This cached execution would be repeated as many times as is needed. > > Now, for your example: > > values = zip(samples, times * num_groups) > if len(values) < len(times) * num_groups: > # raise an error > > Might be done with: > > values = zip( samples, [ lambda:times, ini xrange(num_groups) ] ) > > if len(values) < len(times) * num_groups > > The comma after the lambda is questionable, and this construction would be > slower since lambda automatically invokes the interpreter; but it's correct. How is this any better than the ordinary list comprehension I already suggested as a replacement? For that matter, how is this any better than list multiplication? Your basic complaint about list multiplication as I understand it is that the non-copying semantics are unintuitive. Well, the above is even less intuitive. It is excessively complicated and almost completely opaque. If I were to come across it outside the context of this thread, I would have no idea what it is meant to be doing. > As an aside, how would you do the lambda inside a list comprehension? As a general rule, I wouldn't. I would use map instead. > [lambda:6 for i in xrange(10) ] # Nope. Thak constructs a list of 10 functions and never calls them. If you want to actually call the lambda, then: [(lambda: 6)() for i in range(10)] or: map(lambda i: 6, range(10)) But note that the former creates equivalent 10 functions and calls each of them once, whereas the latter creates one function and calls it ten times. >> Of course you got an integer. You took an index of the range object, not a >> slice. The rule is that taking an index of a sequence returns an element; >> taking a slice of a sequence returns a sub-sequence. You still have not >> shown any inconsistency here. > > > Because it's an arbitrary rule which operates differently than the > traditional idea shown in python docs? > > slice.indices() is *for* (QUOTE)"representing the set of indices specified > by range(start, stop, step)" > http://docs.python.org/2/library/functions.html#slice slice.indices() has nothing to do with it. Indexing a sequence and calling the .indices() method on a slice are entirely different operations. The slice.indices method is a utility method meant to be called by __getitem__ implementations when doing slicing, not an implementation of indexing. When a sequence is indexed, there is no slice. That method is not related in any way to the semantics of indexing a sequence.