Recently, I went to a job interview for a Python developer position. The following code was one of the questions. I just had to write the output.
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 "list2 = %s " %list3
The output is:
list1 = [10, 'a']
list2 = [123]
list2 = [10, 'a']
I'm trying to understand why the first list, list1
, has the 'a'
value.
EDIT
I checked all the links and found out its a python "gotcha" for begginers, but want to thank the answers, can´t choose both so i´m going with the first one.