[Tutor] Program review

Torsten Marek shlomme at gmx.net
Tue Jan 8 00:38:30 CET 2008


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 <shlomme at gmx.net>
ID: A244C858 -- FP: 1902 0002 5DFC 856B F146  894C 7CC5 451E A244 C858
Keyserver: subkeys.pgp.net

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20080108/32e9a029/attachment.pgp 


More information about the Tutor mailing list