[Python-Dev] PEP 340 -- loose ends
Nick Coghlan
ncoghlan at gmail.com
Tue May 3 11:28:40 CEST 2005
Phillip J. Eby wrote:
> Specifically, I propose that PEP 340 *not* allow the use of "normal"
> iterators. Instead, the __next__ and __exit__ methods would be an
> unrelated protocol. This would eliminate the need for a 'next()' builtin,
> and avoid any confusion between today's iterators and a template function
> for use with blocks.
I would extend this to say that invoking the blocktemplate decorator should
eliminate the conventional iteration interface, preventing the following
problematically silent bug:
for l in synchronized(mylock):
# This lock is not released promptly!
break
> My argument is that this is both Explicit (i.e., better than implicit) and
> One Obvious Way (because using existing iterators just Another Way to do a
> "for" loop). It also doesn't allow Errors (using an iterator with no
> special semantics) to Pass Silently.
While I agree these are advantages, a bigger issue for me would be the one
above: keeping a block template which expects prompt finalisation from being
inadvertently used in a conventional for loop which won't finalise on early
termination of the loop.
I'd also suggest that the blocktemplate decorator accept any iterator, not just
generators.
> Of course, since Practicality Beats Purity, I could give this all up. But
> I don't think the Implementation is Hard to Explain, as it should be just
> as easy as Guido's proposal.
I think it would be marginally easier to explain, since the confusion between
iterators and block templates would be less of a distraction.
> Really, the only thing that changes is that you get a
> TypeError when a template function returns an iterator instead of a block
> template, and you have to use the decorator on your generators to
> explicitly label them safe for use with blocks.
I'd add raising a TypeError when a block template is passed to the iter()
builtin to the list of differences from the current incarnation of the PEP.
As for Phillip, I think using different API's is a good way to more clearly
emphasise the difference in purpose between conventional for loops and the new
block statement, but I'm also a little concerned about incorrectly passing a
block template to a for loop.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
More information about the Python-Dev
mailing list