PEP-0315--Enhanced While Loop: An idea for an alternative syntax

Dave K dk123456789 at REMOVEhotmail.com
Thu Feb 19 23:08:11 EST 2004


On 19 Feb 2004 09:29:08 GMT in comp.lang.python, Stefan Axelsson
<sax at csmisc72.cs.chalmers.se> wrote:

(snip)
>
>Generators/iterators gives you e.g. the opportunity to write:
>
>(Snarfed from <674o20dp1kj9ath3hmmha5fvmc342s1gcd at 4ax.com>, by  Dave K
><dk123456789 at REMOVEhotmail.com>) 
>>from sets import Set
>>file('pruned_ips.txt', 'w').writelines(
>>        Set(file('ips.txt')) - Set(file('excluded_ips.txt')))
>>
>>This code relies on `writelines' accepting an iterable, sets returning
>>their members whenever iterated, Set constructors accepting an iterable,
>>and files returning their lines whenever iterated.  And of course, on
>>`close' rarely being needed in Python! :-)

Just for the record, that was actually written by François Pinard, not
me. Well worth quoting again, though, as it's a lovely example.
>
>Something that the assignment as expression doesn't.
>
>I'd say the syntax is just right as it is. No extra complexity in
>while loops or assignments as expressions is needed. In fact the
>relative awkwardness of the present state serves as an excellent
>opportunity to introduce generators/iterators.

FWIW, I think I agree with you. I'm no expert on generators, but I do
know one thing - they're *much* more powerful and flexible than I
currently realise :)

Dave




More information about the Python-list mailing list