[Tutor] Python as Teaching Language

Russel Winder russel at winder.org.uk
Sun Feb 9 14:28:20 CET 2014


On Mon, 2014-01-20 at 10:41 +0000, Oscar Benjamin wrote:
[…]
> f = open('myfile.txt')
> for line in f:
>     print(line.upper())
> f.close()

I suggest we even see this as not good code due to the possibility of
I/O exceptions:

        with open('myfile.txt') as f:
            for line in f:
                print(line.upper())

should, I argue, be the canonical idiom in modern Python.

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



More information about the Tutor mailing list