[Tutor] With

Mike Hansen mhansen@cso.atmel.com
Mon Jun 9 12:47:26 2003


>From: "Alan Gauld" <alan.gauld@blueyonder.co.uk>
>To: <tutor@python.org>,
	"Bob Gailer" <bgailer@alum.rpi.edu>
>Subject: Re: [Tutor] (no subject)
>Date: Mon, 9 Jun 2003 05:09:23 +0100

[...]
>Bob,

>How would a with statement help in Python? I've seen this request
>often for C++ where it could be useful, but with Python's reference
>based naming I've never felt a need for a with in Python?

>I usually just use 'it' as a handy shortcut to any long chains:

>it = foo.bar.baz.whatever
>it.item = 42
>it.another = 27
>it.save()

with foo.bar.baz.whater:
    .item = 42
    .another = 27
    .save()

>and so on...

>What exactly would a 'with' do that an 'it' doesn't?

To me it'd be easier to follow and less noisy. YMMV.

>Alan G
>Author of the Learn to Program web tutor
>http://www.freenetpages.co.uk/hp/alan.gauld

Mike