Looping [was Re: Python and the need for speed]

Ben Bacarisse ben.usenet at bsb.me.uk
Mon Apr 17 21:03:36 EDT 2017


Paul Rubin <no.email at nospam.invalid> writes:

> Ben Bacarisse <ben.usenet at bsb.me.uk> writes:
>>   c = sys.stdin.read(1)
>>   while c == ' ':
>>       c = sys.stdin.read(1)

(for the record: I was not suggesting this was how you'd do it but how
you'd be forced to do it in some languages)

> c = itertools.dropwhile(
>      lambda c: c==' ',
>      iter(lambda: sys.stdin.read(1),None)
>      ).next()

Did you mean

  c = next(itertools.dropwhile(lambda c: c==' ',
                               iter(lambda: sys.stdin.read(1), None)))

?  I get "AttributeError: 'itertools.dropwhile' object has no attribute
'next'" from your example.

Coming from the lazy language Haskell, I find your example natural
(though the syntax is a little busy for my taste) but would it be
considered Pythonic?

-- 
Ben.



More information about the Python-list mailing list