[Python-Dev] type categories
Oren Tirosh
oren-py-d@hishome.net
Wed, 14 Aug 2002 04:43:34 -0400
On Tue, Aug 13, 2002 at 05:15:58PM -0400, Guido van Rossum wrote:
> Alex Martelli introduced the "Look Before You Leap" (LBYL) syndrome
> for your uneasiness with (4) (and (5), I might add -- I don't know
> that __iter__ is always safe). He contrasts it with a different
> attitude, which might be summarized as "It's easier to ask forgiveness
> than permission." In many cases, there is no reason for LBYL
> syndrome, and it can actually cause subtle bugs. For example, a LBYL
> programmer could write
>
> if not os.path.exists(fn):
> print "File doesn't exist:", fn
> return
> fp = open(fn)
> ...use fp...
>
> A "forgiveness" programmer would write this as follows instead:
>
> try:
> fp = open(fn)
> except IOError, msg:
> print "Can't open", fn, ":", msg
> return
> ...use fp...
So far I have proposed two "forgiveness" solutions to the re-iterability
issue:
One was to raise an error if .next() is called after StopIteration so an
attempt to iterate twice over an iterator would fail noisily. You have
rejected this idea, probably because too much code depends on the current
documented behavior.
My other proposed solution is at
http://mail.python.org/pipermail/python-dev/2002-July/026960.html
I suspect it got lost in the noise, though.
Oren