Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'context': 0.04; 'operator': 0.04; 'motivated': 0.05; 'backwards': 0.07; 'behavior.': 0.07; 'currently,': 0.07; 'pypy': 0.07; 'raised': 0.07; 'statically': 0.07; 'suggesting': 0.07; 'tends': 0.07; 'type,': 0.07; 'typed': 0.07; 'python': 0.08; 'dynamically': 0.09; 'etc).': 0.09; 'generators': 0.09; 'subclass': 0.09; 'successive': 0.09; 'subject:python': 0.10; 'read.': 0.10; 'classes,': 0.13; '16,': 0.15; 'case.': 0.15; 'converting': 0.15; 'intermediate': 0.15; 'cpython,': 0.16; 'generators.': 0.16; 'identifiers.': 0.16; 'iterators,': 0.16; 'objection': 0.16; 'preexisting': 0.16; 'preserved': 0.16; 'returned,': 0.16; 'stumbling': 0.16; 'subject:case': 0.16; 'vectors': 0.16; 'workaround': 0.16; '\xc2\xa0i': 0.16; 'cc:addr:python-list': 0.16; 'examples': 0.16; 'wrote:': 0.18; 'interesting.': 0.18; 'simpler': 0.18; 'convert': 0.19; 'slightly': 0.19; 'primarily': 0.21; 'maybe': 0.21; 'dec': 0.22; 'stuff': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'converts': 0.23; 'incorrect': 0.23; 'modification': 0.23; 'cc:2**0': 0.24; "people's": 0.24; 'stack': 0.24; 'code': 0.25; 'function': 0.27; 'skip:[ 10': 0.27; 'url:mailman': 0.28; 'compare': 0.28; 'subject:" ': 0.28; '(you': 0.28; 'lists': 0.28; 'message-id:@mail.gmail.com': 0.28; 'elements': 0.29; 'generic': 0.29; 'mapping': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'correct': 0.29; 'received:209.85.210.46': 0.30; 'received:mail- pz0-f46.google.com': 0.30; 'throwing': 0.30; 'anyone': 0.31; 'developers': 0.32; 'cases': 0.32; 'done,': 0.32; 'implementing': 0.32; 'list': 0.32; 'url:listinfo': 0.32; 'idea': 0.32; 'pretty': 0.32; "can't": 0.32; "isn't": 0.33; 'rather': 0.33; 'there': 0.33; 'object': 0.33; 'fri,': 0.34; 'on,': 0.34; 'done': 0.34; 'community': 0.34; 'creates': 0.34; 'discussed': 0.34; 'realize': 0.34; 'certain': 0.34; 'options:': 0.34; 'preserve': 0.34; 'operations': 0.35; 'list.': 0.35; 'subject:/': 0.35; 'lists,': 0.35; 'especially': 0.35; '(for': 0.35; 'skip:" 20': 0.35; 'url:python': 0.36; 'optimization': 0.36; 'subject:lists': 0.36; 'example,': 0.37; 'class.': 0.37; 'but': 0.37; 'list,': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'using': 0.38; 'received:209.85': 0.38; 'couple': 0.38; 'useful': 0.38; 'clearly': 0.39; "i'd": 0.39; 'url:org': 0.39; 'missing': 0.40; 'hope': 0.61; 'type': 0.61; '2011': 0.61; 'matter': 0.61; 'types': 0.61; 'subject': 0.62; 'direction.': 0.64; 'movement': 0.64; 'subject.': 0.64; 'believe': 0.65; 'here': 0.65; 'placed': 0.67; 'stated': 0.68; 'arms': 0.73; 'factory': 0.73; 'informative': 0.73; 'skip:\xc2 10': 0.74; 'analysis': 0.77; 'strategies': 0.82; '12:48': 0.84; 'habit': 0.84; 'kills': 0.84; 'nathan': 0.84; 'subject:Making': 0.84; 'hooked': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=LYa5n0irznC4T6p6c3mgkh+cYO4Fm5GqqfStLlPMQRQ=; b=dAHPWwlaZBF+onrt/CXPIwLf1HxdH2LvLC0e09RFDa3Amww2Nin7LkhNPfWSi/2Z/i t/fBqTpcdXjthWU9mlD2/JXVbIfcW/8qvsjl34byPfXuvImd+GRdtbvazoF0XQZiUwN4 nnr71nxKgLMlBAfrcDp8IATEkWqpXU32xdbmk= MIME-Version: 1.0 In-Reply-To: References: From: Devin Jeanpierre Date: Fri, 16 Dec 2011 14:03:44 -0500 Subject: Re: Making the case for "typed" lists/iterators in python To: Nathan Rice Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list 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: 110 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324062269 news.xs4all.nl 6875 [2001:888:2000:d::a6]:45365 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17378 > 2. One comprehension: > > L2 =3D [Z(X(Y(e))) for e in L1] > or > L2 =3D [e.X().Y().Z() for e in L1] I want to say that maybe this is better done by functions like map() (although, Python is missing a composition operator which might be handy, and mapping method calls isn't pretty). I don't know that it belongs as a method on lists, especially since that means you can't do the same thing on, for example, generators. Or I guess, the easy answer to that lattermost objection is to make it typed iterators rather than typed lists. -- Devin On Fri, Dec 16, 2011 at 12:48 PM, Nathan Rice wrote: > I realize this has been discussed in the past, I hope that I am > presenting a slightly different take on the subject that will prove > interesting. =C2=A0This is primarily motivated by my annoyance with using > comprehensions in certain circumstances. > > Currently, if you want to perform successive transformations on the > elements of a list, a couple of options: > > 1. Successive comprehensions: > > L2 =3D [X(e) for e in L1] > L3 =3D [Y(e) for e in L2] > L4 =3D [Z(e) for e in L3] > or > L2 =3D [e.X() for e in L1] > > This gets the job done and gives you access to all the intermediate > values, but isn't very succinct, particularly if you are in the habit > of using informative identifiers. > > 2. One comprehension: > > L2 =3D [Z(X(Y(e))) for e in L1] > or > L2 =3D [e.X().Y().Z() for e in L1] > > This gets the job done, but doesn't give you access to all the > intermediate values, and tends to be pretty awful to read. > > Having "typed" lists let you take preexisting string/int/etc methods > and expose them in a vectorized context and provides an easy way for > developers to support both vectors and scalars in a single function > (you could easily "fix" other people's functions dynamically to > support both). =C2=A0Additionally, "typed" lists/iterators will allow > improved code analysis and optimization. =C2=A0The PyPy people have alrea= dy > stated that they are working on implementing different strategies for > lists composed of a single type, so clearly there is already community > movement in this direction. > > Just compare the above examples to their type-aware counterparts: > > L2 =3D X(L1) > L2 =3D L1.X() > > L2 =3D Z(Y(X(L1))) > L2 =3D L1.X().Y().Z() > > Also, this would provide a way to clean up stuff like: > > "\n".join(l.capitalize() for l in my_string.split("\n")) > > into: > > my_string.split("\n").capitalize().join_this("\n") > > Before anyone gets up in arms at the idea of statically typed python, > what I am suggesting here would be looser than that. =C2=A0Basically, I > believe it would be a good idea in instances where it is known that a > list of single type is going to be returned, to return a list subclass > (for example, StringList, IntegerList, etc). =C2=A0To avoid handcuffing > people with types, the standard list modification methods could be > hooked so that if an object of an incorrect type is placed in the > list, a warning is raised and the list converts to a generic object > list. =C2=A0The only stumbling block is that you can't use __class__ to > convert from stack types to heap types in CPython. =C2=A0My workaround fo= r > this would be to have a factory that creates generic "List" classes, > modifying the bases to produce the correct behavior. =C2=A0Then, converti= ng > from a typed list to a generic object list would just be a matter of > removing a member from the bases for a class. =C2=A0This of course > basically kills the ability to perform type specific list optimization > in CPython, but that isn't necessarily true for other implementations. > =C2=A0The additional type information would be preserved for code analysi= s > in any case. =C2=A0The case would be even simpler for generators and othe= r > iterators, as you don't have to worry about mutation. > > I'd like to hear people's thoughts on the subject. =C2=A0Currently we are > throwing away useful information in many cases that could be used for > code analysis, optimization and simpler interfaces. =C2=A0I believe that > "typed" lists that get "demoted" to normal lists with a warning on out > of type operations preserve this information while providing complete > backwards compatibility and freedom. > > Nathan > -- > http://mail.python.org/mailman/listinfo/python-list