[Haskell-cafe] Lambda and closures in PHP -- could someone please comment?

Evan Laforge qdunkan at gmail.com
Wed Jun 18 12:18:29 EDT 2008

> * A closure must only keep alive the varables it references, not the
> whole pad on which they are allocated (Python messed up here)

Getting off subject, but I didn't know this about python.  I'm not
saying you're incorrect, but my experimentation shows:

% cat t.py
class A(object):
    def __init__(self, name): self.name = name
    def __del__(self): print self.name, 'gone'

def f():
    x = A('x')
    y = A('y')
    def g():
        print x.name, 'alive'
    return g

% python
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import t
g = t.f()
y gone
x alive
#-> (<cell at 0xefb50: A object at 0xefb90>,)
del g

x gone

So it looks to me like 'y' is not preserved by the closure returned by
'f', and you can see that in its func_closure which keeps the
reference to one A object, but not two.

Python closures do have a "unique" quirk, which is that variables
bound in a nested function are read-only.  This is something that a
schemer might take issue with, but a haskeller probably wouldn't
notice :)  And for the longest time python had its "two space"
namespace thing going on, so it wasn't even really lexically scoped,
but that's many years ago now.

More information about the Haskell-Cafe mailing list