Q: Python 2.0 preliminary features?

Jeremy Hylton jeremy at cnri.reston.va.us
Mon Oct 11 15:31:22 EDT 1999


  [I wrote:]
  >> def make_adder(num):
  >>     def adder(x):
  >>             global num
  >>             return x + num
  >>     return adder
  >> In this case, adder maintains a reference to make_adder's
  >> environment, and that environment maintains a reference to adder,
  >> because adder was defined there.  So we have a cycle...

>>>>> "TP" == Tim Peters <tim_one at email.msn.com> writes:

  TP> In Greg's scheme (which you may be confusing with John
  TP> Skaller's), you write that in the more Scheme-like way:

(Yes.  I'm definitely getting my Skallers and Ewings confused.  Sorry.)

  TP> def make_adder(num):
  TP>     def adder(x):
  TP>         return x + num
  TP>     return adder

  TP> The "def adder" does *not* create a closure (and hence neither a
  TP> cycle) under Greg's scheme.  It binds a new kind of object to
  TP> "adder", a trivial wrapper around the function object currently
  TP> stored.  No info about bindings is contained in this new object.
  TP> The sole purpose of the new object is to force a *later*
  TP> operation to create a closure.

Ok.  I think I've got it now.  These are the read-only variant of
closures, right?  So the following will not work?

def make_accumulator():
    sum = 0
    def accumulate(x):
	sum = sum + x
	return sum
    return accumulate

Of course, there isn't much lost here, because I can spell this in
Python a lot more easily by starting with "class."

  TP> I went into some detail on when Greg's patch would and wouldn't
  TP> create cycles in an earlier (but still very recent) msg, and
  TP> didn't feel like repeating it all.

now-on-to-reading-the-earlier-messages-later-ly y'rs,
Jeremy




More information about the Python-list mailing list