[Python-Dev] Python FAQ: Why doesn't Python have a "with" statement?

"Martin v. Löwis" martin at v.loewis.de
Sat Jun 14 08:55:58 CEST 2008

> the block defined by the "on" statement first must starts looking at
> the object's namespace. If no symbol was defined inside a, then it
> follows the traditional LEGB name resolution.
> Assignament must work on the object's namespace, of course:

This probably belongs to python-ideas or some such, but I don't
think this approach can work. People will want to assign to local
variables in an "ob" block, and then be surprised that the
assignment actually modified their object:

def f(L):
    total = 0
    for h in L:
        on h:
            more code accessing h's attributes
            if x: # reads h.x
                total = total+1
    return total

People will be surprised that total is always 0 (and that
some objects have an attribute total with a value of 1).

on x:
    for e in L:
        counts[e] += 1 # modifies x.counts

People will be surprised that x also grows an attribute
e, as the for loop involves an assignment, which you say
goes to the object's namespace, of course.


More information about the Python-Dev mailing list