what is a closure?

Steve Horne sh at ttsoftware.co.uk
Mon Dec 18 06:30:03 EST 2000


On Fri, 15 Dec 2000 17:39:11 GMT, Jeremy Hylton <jeremy at alum.mit.edu>
wrote:

>A closure provides a way to resolve free variables in a function body;
>it is created when the function is defined. Currying is a way to create
>a binding for a formal parameter; it is used when a function is called

OK, so if I could write...

  def Test (a) :
    return lambda p : p + a

in Python, then this would be a closure.

However, what if I write...

  def Test :
    a = 1
    b = lambda p : p + a

    a = 2
    c = lambda p : p + a

    print b (1)
    print c (1)

Would the result be...

  2
  3

Or would it be...

  3
  3

In short, for the standard behaviour of a closure, would the value of
a be bound at the time the lambda is defined, or is some kind of
reference created.

-- 
Steve Horne
Home : steve at lurking.demon.co.uk
Work : sh at ttsoftware.co.uk



More information about the Python-list mailing list