I was just playing with python list and I found strange behavior.
Here is my code.
def extendList(val, list=[]):
list.append(val)
return list
list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList('a')
print "list1 = %s" % list1
print "list2 = %s" % list2
print "list3 = %s" % list3
Output :
list1 = [10, 'a']
list2 = [123]
list3 = [10, 'a']
What I expect to believe is that list 3 output can contain 10 with 'a' as 10 was already in list variable of function argument when we called list1=extendList(10)
(because expressions in default arguments are calculated when the function is defined, not when it’s called.) But how come 'a' gets appended to list1 as we are not calling it second time.