Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Jussi Piitulainen Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Tue, 22 Mar 2016 15:32:05 +0200 Organization: A noiseless patient Spider Lines: 37 Message-ID: References: <56e44258$0$1598$c3e8da3$5496439d@news.astraweb.com> <8737rvxs89.fsf@elektro.pacujo.net> <56e7483d$0$1608$c3e8da3$5496439d@news.astraweb.com> <56f09973$0$1601$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="305c68510616a2e7ac08bcd2ff1598bd"; logging-data="2771"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BSZQl3e5uv0zo4LrSk422PK6WnBbu3JA=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:GO8k2ukvNpQtSFlQYoXsmC+2XT4= sha1:sdSvVyLfzeSQLzu5eqo0uTHbeiY= Xref: csiph.com comp.lang.python:105484 Chris Angelico writes: > On Wed, Mar 23, 2016 at 12:15 AM, Jussi Piitulainen wrote: >> Chris Angelico writes: >> >>> Or use filter(), which is sometimes clearer: >>> >>> # You probably want a more sophisticated function here >>> def nonspace(ch): return not ch.isspace() >>> >>> next(filter(nonspace, stream)) >> >> Sure. >> >> # But there's more fun hiding in the standard library. >> next(itertools.filterfalse(operator.methodcaller('isspace'), stream)) > > ... at that point, the genexp is miles ahead in readability :) ;) > Although I do sometimes yearn for a "filterout" function that does the > same thing as filter() but negates its predicate. Then you could use: > > next(filterout(str.isspace, stream)) > > to say "give me the next from the stream, filtering out those which > are spaces". It's not hard to write, of course. from itertools import filterfalse as filterout next(filterout(str.isspace, """ I didn't know str.isspace works like that! """)) ---> 'I'