setting variables in outer functions

Diez B. Roggisch deets at nospam.web.de
Wed Oct 31 20:36:26 EDT 2007


Hrvoje Niksic schrieb:
> "Chris Mellon" <arkanes at gmail.com> writes:
> 
>> I have no idea why someone who already has a working, object system
>> would want to implement their own on top of closures.
> 
> This subthread is getting ridiculous -- closures are *not* useful only
> for implementing object systems!  The object system thing is just a
> method of teaching abstractions popularized by SICP.  Abstractions
> normally taken for granted, such as objects, or even expression
> evaluation, are implemented from scratch, using only the most basic
> primitives available.  In a Lisp-like language with lexical scope, the
> most basic storage primitive is the lexical environment itself[1].
> 
> In real-life code, closures are used to implement callbacks with
> automatic access to their lexical environment without the need for the
> bogus additional "void *" argument one so often sees in C callbacks,
> and without communication through global variables.  If the callbacks
> can access variables in the outer scope, it's only logical (and
> useful) for them to be able to change them.  Prohibiting modification
> reduces the usefulness of closures and causes ugly workarounds such as
> the avar[0] pattern.
> 
> If closures were useful only for implementing bogus object systems,
> neither they nor "nonlocal" would have made it to Python in the first
> place.
> 

There's a nice saying about this:

"""
   The venerable master Qc Na was walking with his student, Anton. 
Hoping to
prompt the master into a discussion, Anton said "Master, I have heard that
objects are a very good thing - is this true?"  Qc Na looked pityingly at
his student and replied, "Foolish pupil - objects are merely a poor man's
closures."

   Chastised, Anton took his leave from his master and returned to his cell,
intent on studying closures.  He carefully read the entire "Lambda: The
Ultimate..." series of papers and its cousins, and implemented a small
Scheme interpreter with a closure-based object system.  He learned much, and
looked forward to informing his master of his progress.

   On his next walk with Qc Na, Anton attempted to impress his master by
saying "Master, I have diligently studied the matter, and now understand
that objects are truly a poor man's closures."  Qc Na responded by hitting
Anton with his stick, saying "When will you learn? Closures are a poor man's
object."  At that moment, Anton became enlightened.
"""

http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html


I do love closures, and I sometimes pitty python for not allowing to set 
outer scope variables.

But then, it's because it lacks a variable declaration scheme (as e.g. 
javascript has), so _some_ design decision had to be made distinguishing 
variable declarations from pure references. And I can live with the 
consequences.

Diez



More information about the Python-list mailing list