Weird lambda rebinding/reassignment without me doing it

ssecorp circularfunc at gmail.com
Thu Jul 10 15:27:10 CEST 2008


I am never redefining the or reassigning the list when using validate
but since it spits the modified list back out that somehow means that
the modified list is part of the environment and not the old one.
i thought what happend inside a function stays inside a function
meaning what comes out is independent of what comes in.
Meaning if I want the list I send as a parameter to the function I
have to do x = func(x) and not just func(x) and x is magically what
comes out of func().
Doesnt Python have closure or that isnt what this is about?


def validate(placed):
    student = round(random.random()*401)
    if student in placed:
        return validate(placed)
    else:
        placed.append(student)
        return student, placed

def val(placed):
    student = round(random.random()*401)
    if student in placed:
        return validate(placed)
    else:
        placed.append(student)
        return student



>>> g = lambda x:validate(x)
>>> l=[]
>>> for x in range(1,10):
	g(l)


(141.0, [141.0])
(19.0, [141.0, 19.0])
(86.0, [141.0, 19.0, 86.0])
(120.0, [141.0, 19.0, 86.0, 120.0])
(76.0, [141.0, 19.0, 86.0, 120.0, 76.0])
(262.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0])
(234.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0, 234.0])
(74.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0, 234.0, 74.0])
(325.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0, 234.0, 74.0, 325.0])
>>> g = lambda x:val(x)
>>> l=[]
>>> for x in range(1,10):
	g(l)


183.0
33.0
315.0
244.0
308.0
168.0
146.0
378.0
297.0
>>>



More information about the Python-list mailing list