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

Stefan Axelsson sax at csmisc72.cs.chalmers.se
Thu Feb 19 04:29:08 EST 2004


In article <7xhdxnzs8a.fsf at ruckus.brouhaha.com>, Paul Rubin wrote:

> Yeah, except now you've bloated up your code with another function.
> Assignment expression solves this one too.

Except that the 'bloat' is constant, you only need one function, once
to solve all your 'read'-problems. And hence that function probably
belongs in a library, like it does for 'readline', I have no doubt
that it'll end up there if this situation turns out to be a problem.

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! :-)

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.

Stefan,
-- 
Stefan Axelsson                  (email at http://www.cs.chalmers.se/~sax)



More information about the Python-list mailing list