[Python-Dev] context manager - generator interaction?
steve at holdenweb.com
Fri Apr 6 13:18:26 CEST 2007
Guido van Rossum wrote:
> On 4/5/07, Raymond Hettinger <python at rcn.com> wrote:
>>> Guido van Rossum wrote:
>>>> Isn't this violating the rule that a try/except should only enclose
>>>> the smallest expression where the exception is expected?
>>> Yeah, and I keep finding myself wanting to hyperlink to that "rule" in
>>> the official docs, but it only seems to be written down in developer's
>>> heads. Can we get that into the Language Ref somewhere? Maybe on the
>>> http://docs.python.org/ref/try.html page?
>> I'll add a snippet to the tutorial with advice on try/except.
>> I don't think it should go in the main documentation because it is
>> really more of a suggestion or technique than an actual rule.
>> Also, there are plenty of other situations where the "rule" doesn't apply.
> I see a lot of code written by Googlers who are not (yet) experienced
> Python users. Overly wide try/except clauses are a common mistake, and
> it is nearly always easy to apply this rule. I would argue, as a style
> issue, for making this rule the norm, and require deviations to be
> thought through very carefully.
> This rule has no place in a pure language *reference* manual. But it
> certainly deserves mention in any form of more practical
> documentation, be it a tutorial or a more advanced programming manual.
PEP 8 would (also? I don't see this being relevant to the reference
manual, but that's not a biggie) seem a logical place for this
information to go, since it deals with try-except. I'd suggest something
along the lines of:
A frequent beginner's error is to make the scope of a try clause too
broad, covering a larger span of code that necessary so that the except
clause is triggered by unanticipated exceptions. Try to ensure that a
try clause applies to the fewest possible statements to avoid these issues.
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
More information about the Python-Dev