'with' statement in python ?

piet at cs.uu.nl piet at cs.uu.nl
Tue Jun 26 11:18:42 EDT 2001


>>>>> macias at bp.com.pl (Maciej Pilichowski) (MP) writes:

MP> On Mon, 25 Jun 2001 21:30:36 -0300, Carlos Ribeiro
MP> <cribeiro at mail.inet.com.br> wrote:

MP> I have just started learning Python yesterday but...

>> def with_is_broken(a):
>> with a:
>> print x
>> 
>> The snippet above assume that "a" must have a member attribute called "x". 

MP> Nope. As you referred to Pascal -- if "a" has a member "x" it is read
MP> as "a.x", if not it is standalone "x". 

Python, as it is now has three environments from which it identifies
variables:
1. builtin
2. the current module (these 2 together are called global variables)
3. the function (these are called local variables, and in Python 2.1+ these
   can be nested).

If `with' would be implemented there would be a fourth environment, namely
the dict of the object given after `with'. It would probably slow down
things if you also used local and/or global variables in the `with' block,
on the other hand if you used only instance variables it could speed up
things a little.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list