[Python-Dev] Scope, not context? (was Re: PEP 550 v3 naming)

Antoine Pitrou solipsis at pitrou.net
Fri Aug 25 06:28:08 EDT 2017


On Fri, 25 Aug 2017 15:36:55 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 24 August 2017 at 23:52, Barry Warsaw <barry at python.org> wrote:
> > Guido van Rossum wrote:  
> >> On Tue, Aug 22, 2017 at 7:12 PM, Nathaniel Smith <njs at pobox.com> wrote:
> >>
> >> I worry that that's going to lead more people astray thinking this has
> >> something to do with contextlib, which it really doesn't (it's much more
> >> closely related to threading.local()).  
> >
> > This is my problem with using "Context" for this PEP.  Although I can't
> > keep up with all names being thrown around, it seems to me that in
> > Python we already have a well-established meaning for "contexts" --
> > context managers, and the protocols they implement as they participate
> > in `with` statements.  We have contextlib which reinforces this.  What's
> > being proposed in PEP 550 is so far removed from this concept that I
> > think it's just going to cause confusion (well, it does in me anyway!).  
> 
> While I understand the concern, I think context locals and contextlib
> are more closely related than folks realise, as one of the main
> problems that the PEP is aiming to solve is that with statements (and
> hence context managers) *do not work as expected* when their body
> includes "yield", "yield from" or "await" .

If I write:

def read_chunks(fn, chunk_size=8192):
    with open(fn, "rb") as f:
        while True:
            data = f.read(chunk_size)
            if not data:
               break
            yield data

The "with" statement here works fine even though its body includes a
"yield" (and if there had been an added "await" things would probably
not be different).

The class of context managers you're talking about is in my experience a
small minority (I've hardly ever used them myself, and I don't think I
have ever written one).  So I don't think the two concepts are as
closely related as you seem to think.

That said, I also think "context" is the best term (barring
"environment" perhaps) to describe what PEP 550 is talking about.
Qualifying it ("logical", etc.) helps disambiguate

Regards

Antoine.




More information about the Python-Dev mailing list