[Python-ideas] Object grabbing

Steven D'Aprano steve at pearwood.info
Mon May 2 21:40:56 EDT 2016

On Mon, May 02, 2016 at 09:25:01PM +0100, Erik wrote:
> On 02/05/16 14:57, Koos Zevenhoven wrote:
> >On Mon, May 2, 2016 at 3:44 PM, Alexander Walters
> ><tritium-list at sdamon.com> wrote:
> >>with open(foo), open(bar):
> >>     .write(baz)  # does what?

open(bar) could shadow open(foo), just like in this:

with open(foo):
    .write(baz)  # refers to foo
    with open(bar):
        .write(baz)  # refers to bar, shadowing foo

There's no ambiguity here: last one seen wins, the same rule used all 
over Python:

import math as m, string as m, urllib as m, decimal as m
x, x, x, x = 1, 2, 3, 4

etc. So I don't think this is an insurmountable problem.

> >Hopefully it would raise an exception,
> Then you are suggesting that an attribute is looked up at runtime on 
> _all_ objects declared in all live 'with' scopes every time one is 
> referenced in order to determine if it's an ambiguous reference (at the 
> time of that reference).
> O(n). I'm pretty sure that will never happen.

But for very small n. I mean, if you somehow manage to nest five or six 
hundred with statements, you probably deserve whatever happens to you... 



More information about the Python-ideas mailing list