'with' statement in python ?

Andrew Henshaw ahenshawatapowerdotcom
Tue Jun 26 23:07:14 EDT 2001


"Carlos Ribeiro" <cribeiro at mail.inet.com.br> wrote in message
news:mailman.993562981.5640.python-list at python.org...
> At 11:49 26/06/01 +0000, Maciej Pilichowski wrote:
> >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".
> >
> >Nope. As you referred to Pascal -- if "a" has a member "x" it is read
> >as "a.x", if not it is standalone "x".
>
> There must be some misunderstanding here. My snippet (incomplete, as I
> pointed out) assumed that Python had a "with" keyword, just to show the
> kind of problems that arise. In Python, the compiler/interpreter has no
way
> to tell beforehand if x is a member of a, a local variable, a module level
> variable, or a global variable. This ambiguity makes the use of with in
> Python impossible.
>
...snip...
I'm not a 'with' advocate, but if I was, I'd suggest:

def with_maybe_not_broken(a):
    with a:
        print  .x

This seems like it would fix the concerns; however, you wouldn't be able to
nest 'with' statements.

Andrew Henshaw





More information about the Python-list mailing list