On Mo, 2008-01-07 at 21:15 -0300, Ricardo Aráoz wrote:
> Kent Johnson wrote:
> > Ricardo Aráoz wrote:
> >> PEP 0343 is not an
> >> example of clarity in the definition of a statement, it mixes
> >> justification with historic development with definition with actual
> >> equivalent code. Couldn't or wouldn't bother to understand it.
> > 
> > Here is a better starting point:
> > http://docs.python.org/whatsnew/pep-343.html
> > 
> > Kent
> > 
> 
> Thanks Kent, it's clearer, but it gets rapidly complicated. Anyway
> looking at the simpler use, if you would have exception handling in :
> 
> try :
>     with open('/etc/passwd', 'r') as f:
>         for line in f:
>             print line
>             ... more processing code ...
> except ExceptionsOnOpening :
>     ... exception handling
> except :
>     ... exceptions inside the for
> 
> 
> 
> Whereas traditionally :
> 
> try :
>     f = open('/etc/passwd', 'r')
>     for line in f:
>         print line
>         ... more processing code ...
> except ExceptionsOnOpening :
>     ... exception handling
> except :
>     ... exceptions inside the for

Hi Ricardo,

don't forget the "f.close()" call after the for loop, in a finally block
[0]. Context managers are all about generalizing non-adjacent parts of
setup and teardown code  

Actually, the fact that you forgot the close call is an argument for
using the context manager;)

best,

Torsten
[0] http://docs.python.org/lib/bltin-file-objects.html
-- 
Torsten Marek <[EMAIL PROTECTED]>
ID: A244C858 -- FP: 1902 0002 5DFC 856B F146  894C 7CC5 451E A244 C858
Keyserver: subkeys.pgp.net

Attachment: signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to