Thoughts Heap

A Blog by Roman Gonzalez.-

RSS
Mar
31st
Wed
permalink

Default mutable values in Python functions are WACK!

If you use mutable default values on functions, this will keep it state in recurrent calls:


def myAppend(x, L=[]):
    L.append(x)
    return L

#=============

>>> myAppend(1)
=> [1]
>>> myAppend(2)
=> [1, 2]

That’s seriously messed up o.O

blog comments powered by Disqus