[Python-Dev] closure semantics

Alex Martelli aleaxit at yahoo.com
Sat Oct 25 09:56:54 EDT 2003


On Wednesday 22 October 2003 07:34 pm, Michael Chermside wrote:
> [Jeremy]
>
> > I'm not averse to introducing a new keyword, which would address both
> > concerns.  yield was introduced with apparently little problem, so it
> > seems possible to add a keyword without causing too much disruption.
> >
> > If we decide we must stick with global, then it's very hard to address
> > Alex's concern about global being a confusing word choice <wink>.
>
> [Guido]
>
> > OK, the tension is mounting.  Which keyword do you have in mind?  And
> > would you use the same keyword for module-globals as for outer-scope
> > variables?
>
> Surely the most appropriate keyword is "scope", right?

That is my personal vote, yes.


> As in
>
>    scope a is global
>    scope b is nested
>    scope c is self
>    scope d is myDict
>
> Okay... maybe I'm getting too ambitious with the last couple...

If we have to have 'scope' as a statement, I'd slightly prefer it if it HAD 
something useful to do, so I understand your ambition.

If somebody thinks it's useful and important to be able to spell
    themodule.x = 23
as
    <some declarative statement about> x    # e.g. at top of function
    ...many lines in-between obscuring the issue...
    x = 23
then it WOULD no doubt be consistent to be able to do similar things
for other "whatever.x = 23" assignments.

However, that "myDict" still leaves me dubious.  If we want to make
it easy to use attribute setting and access syntax in lieu of dictionary
indexing syntax (and it does look nicer often enough) then it seems
to me that we should rather make available a fast equivalent of a
wrapper such as

class ItemsAsAttrs(object):
    def __init__(self, d):
        object.__setattr__(self, 'd', d)
    def __getattr__(self, n):
        return self.d[n]
    def __setattr__(self, n, v):
        self.d[n] = v

then, "scope xx is ItemsAsAttr(myDict)" would work if such things
as "scope xx is self" did.


Personally, I'd rather go in the other direction: make all assignments
except those to local variables into something _locally clear without
needing to look for possible declarations who knows where_ , rather
than fall for the "convenience trap" of allowing assignment to bare
names (and presumably "side-effect rebindings" such as those in
statements def, class, for, import) to mean something different
depending on "declarative statements".

However, I do understand that it would at least be consistent to
allow such "insidious convenience" for many kinds of non-local
names, as your "ambitious proposals" imply/suggest.  If it IS deemed
desirable to give "x=23" semantics that depend on the possible
presence of a "declarative statement" who-knows-where, it seems
consistent to allow that "convenience" for all kind of semantics.

Perhaps an idea which I think Samuele suggested might be less
insidious that allowing the "declarative statement" to be just
about anywhere within the current function: make 'scope' a normal
compound statement, as in, e.g.:

    scope x in module, xx in foo, z, t, v in self:
        x = 23
        ...etc etc...

this way, at least, the semantics of "x = 23" depend only on those
declarative statements *it's nested inside of*; better than having to
look all over the function, before AND after the "x = 23" and inside
flow control statements too (!), for the "global x" (or whatever) that
MIGHT make "x = 23" mean something different.


Alex




More information about the Python-Dev mailing list