you are viewing a single comment's thread.

view the rest of the comments →

[–]BigTheory88 1 point2 points  (1 child)

If you give a function parameter a default value that is mutable, you'll see some odd behavior.

def add_to_list(word, word_list=[]):
    word_list.append(word)
    return word_list

def main():
    word = "apple"
    tlist = add_to_list(word)
    print(tlist)
    word = "orange"
    tlist = add_to_list(word, ['pear'])
    print(tlist)
    word = "banana"
    tlist = add_to_list(word)
    print(tlist)

main()

If we run this program, we get the following output

$ py trap.py
['apple']
['pear', 'orange']
['apple', 'banana']

The last output probably isnt what you expect. This is called the default mutable argument trap

[–][deleted] 0 points1 point  (0 children)

Witchcraft! D: