generator slides review

Peter Otten __peter__ at web.de
Sun Feb 2 07:47:06 EST 2014


andrea crotti wrote:

> 2014-02-01 Miki Tebeka <miki.tebeka at gmail.com>:
>>
>> My 2 cents:

>> slide 21:
>> from itertools import count, ifilterfalse
>>
>> def divided_by(p):
>>     return lambda n: n % p == 0
>>
>> def primes():
>>     nums = count(2)
>>     while True:
>>         p = next(nums)
>>         yield p
>>         nums = ifilterfalse(divided_by(p), nums)
>>
> 
> Thank you that's nicer, 

It may be nice, but is probably less efficient because of the lambda 
function calls that replace the if expression in your

> def exclude_multiples(n, ints):
>     for i in ints:
>         if (i % n) != 0:
>            yield i

> but ifiilterfalse is not in Python 3 (could
> use filter of course).

ifilterfalse() isn't gone in Python3, it just was renamed to filterfalse().





More information about the Python-list mailing list