Python "with"

Ivan Voras ivoras at __fer.hr__
Sun Sep 16 22:07:07 CEST 2007


Hi,

I'm looking for a construct that's similar to (Turbo) Pascal's "with"
statement. I read about the Python's new "with" statement, but I was
dissapointed to learn that it does something different (I still don't
see how it's better than try..except..finally, but that's not my question).

Is there something similar to what I want that's Pythonic enough?

(If you're not familiar with Pascal, here's how it works:

with some.big.structure.or.nested.objects.element do begin
     member1 := something;
     member2 := something;
end;

which exactly translates to this equivalent:

some.big.structure.or.nested.objects.element.member1 := something;
some.big.structure.or.nested.objects.element.member2 := something;

i.e. it's a wrist-saver. I think a similar feature exists in C99, for
structs.

I know it can be almost always done by using a temporary variable:

tmp = some.big.structure.or.nested.objects.element
tmp.member1 = something
tmp.member2 = something

but this looks ugly to me.)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 258 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20070916/6b9592dd/attachment.pgp>


More information about the Python-list mailing list