'with' statement in python ?

Guido van Rossum guido at python.org
Tue Jun 26 22:58:12 CEST 2001


D-Man <dsh8290 at rit.edu> writes:

> It's not impossible, it would just be a runtime determination.
> Already for bare names python looks in locals(), then up through the
> nested scopes (if enabled) and in globals() and __builtin__.  It
> _could_ do the same for instances in the presence of a 'with'
> statement.

Note that for *reading* variables Python searches a bunch of 
scopes: locals, globals, builtins.  (Plus outer functions if nested
scopes are enabled in Python 2.1.)

But for *writing* variables, Python has to pick one scope, and it
always picks the innermost scope.  The with statement would have to
make all simple assignments assign to an attribute of the object
mentioned in the with clause.

I don't think this would enhance code clarity -- several examples have
already been posted.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list