Closures in leu of pointers?

Terry Reedy tjreedy at udel.edu
Sat Jun 29 16:53:35 EDT 2013


On 6/29/2013 3:47 PM, Ian Kelly wrote:
> On Sat, Jun 29, 2013 at 1:33 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> On Sat, 29 Jun 2013 12:20:45 -0700, cts.private.yahoo wrote:

>> Huh? What language are you programming in? Python doesn't have implied
>> scoping in non-intuitive ways.
>
> def f(x):
>      def g(y):
>          print(x)
>          x = y
>
> Within g, the variable x is implicitly local,

The assignment looks pretty explicit to me ;-|. But to each his own on 
'plicitness.

> which is non-intuitive
> since without the assignment it would not be.

I think the problem people have is this. Python is *not* an interpreted 
language in the traditional sense: read a line, interpret and execute 
it. It is compiled, and the compilation of functions is a two pass 
affair. The first pass classifies all names. The second pass generates 
code based on the classification of the first pass. This is not part of 
the grammar, but implied (required) by the semantic description.

If, in the general case, the compiler requires two passes to understand 
a function body, then *so do people*#. This requirement is what trips up 
people who are either not used to the idea of two-pass compilation or do 
not cognize that the language requires it.

# The alternative for either program or people is a 1-pass + 
backtracking process where all understandings are kept provisional until 
the end of the body and revised as required. 2 passes are simpler.

That said, writing deceptive but functionin code is usually bad style. 
Writing code that looks functional but is buggy is worse.

-- 
Terry Jan Reedy




More information about the Python-list mailing list