[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] PEP 572 semantics

[Steve Dower]
> Okay, so as far as the specification goes, saying "assignment

> > expressions in comprehensions get or create a cell variable in the

> > defining scope and update its value" satisfies me just fine (or some

> > other wording that more closely mirrors the actual behaviour - all my

> > work here is on my own compiler, not the actual CPython one, and I don't

> > know that they're identical).

> >

> > I don't think this should be left assumed by the PEP. If it's likely to

> > be a restriction on other implementations to say "cell variable", then

> > say "For example, in CPython, ..."

The problem:  nothing about Python's scoping is changed by this PEP, so
saying anything related to the implementation of scoping risks giving a
misleading impression that something about scoping _is_ being changed.

The PEP certainly needs to define the intended scope of
assignment-expression targets in all cases.  It already tried, with perhaps
too few words.  But it strikes me as counterproductive to go on to say
anything about how scopes are implemented.  If, e.g., the PEP is clear that

    def f():
        return (a := i for i in range(5))

`a` is local to `f` and `i` is local to a directly nested scope, what's
left to the imagination?  Well, the implementations of scopes and
closures.  None of which the PEP changes.  The semantics of `i` haven't
changed from what we already have, neither the visibility nor extent of the
generator expression object returned.

The only new thing is specifying the scope of `a`, where "local to f" means
exactly the same thing as for any other name local to a function today.  So
far as the PEP semantics go, it doesn't even matter whether an
implementation _does_ implement some form of closure as such.  It just has
to provide the visible semantics of *lexically* nested scopes with
indefinite extent, by whatever means it likes best.  That's what "local to
f" means (and has meant all along - well, since lexically nested scopes
were first introduced).

Would it help to say "and by 'local to f' we mean the same thing we meant
before this PEP was written"?  Probably not ;-)  If someone doesn't know
what "name N belongs to scope S" means, that needs to get a clear answer,
but it's a fundamental question that's quite independent of this particular

That said, I bet it would be helpful to give some examples that show how
CPython intends to implement this stuff, via giving explicit nested
workalike Python functions decorated with scope declarations.  The
concreteness of that is helpful.  The eternal problem with that is finding
a way to stop readers from taking the "workalike functions" too literally
(e.g., "but i tried it and it doesn't generate exactly the same byte
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180704/328d8b08/attachment.html>