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

Re: Closures



Thanks, Samuele!  Very interesting.  Unfortunately, I don't quite
understand it.

Samuele Pedroni <pedroni@inf.ethz.ch> wrote:

> > The Python "idiomatic way to get the same" below does not seem to
> > close over local variables at all.
> > ...
> 
> It closes but still you can't assign:
> 
> def make_counter_with_incr(inc):
>   class ctr:
>     def __init__(self):
>        self.n = 0
>     def __call__(self):
>        self.n = self.n + inc
>        return self.n
>    return ctr
> 
> c=make_counter_with_incr(4)
> print c()  # 4
> print c()  # 8

OK so far ...

> Here the rationale for Python, mostly a GvR (aka Python BDFL) point:
> 
> >     There are technical issues that make it difficult to support
> >     rebinding of names in enclosing scopes, but the primary reason
> >     that it is not allowed in the current proposal is that Guido is
> >     opposed to it.  His motivation: it is difficult to support,
> >     because it would require a new mechanism that would allow the
> >     programmer to specify that an assignment in a block is supposed to
> >     rebind the name in an enclosing block; presumably a keyword or
> >     special syntax (x := 3) would make this possible. 

Why is it *rebinding* in enclosing scopes and why would a special
syntax be needed?  (I don't remember very much about Python, I'm
afraid.)

> PS: GvR is known to have said something along the line of: the mind of the
> average Python programmer is simpler than that of average Lisp programmer.
> I think both groups get an appropriate set of tools.

Ok, but the Python approach seems more complex to me, rather than simpler.

-- Jeff