The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

Chris Angelico rosuav at gmail.com
Tue Mar 22 09:38:52 EDT 2016


On Wed, Mar 23, 2016 at 12:32 AM, Jussi Piitulainen
<jussi.piitulainen at helsinki.fi> wrote:
>> 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'

str.isspace(s) <-> s.isspace(), as long as type(s) is str. Very handy
for this sort of thing. And yeah, the import is an option, but if I'm
trying to explain stuff to people, it's usually easier to grab a
genexp (full flexibility, but the complexity) than to play around with
importing. When the function you want exists and returns true for the
things you want, filter() has a big win; for any other situation, it's
not worth reaching to itertools for a specific solution when the
generic one will cover this and every other case.

ChrisA



More information about the Python-list mailing list