Skip to content

Latest commit

 

History

History
34 lines (21 loc) · 580 Bytes

Python.md

File metadata and controls

34 lines (21 loc) · 580 Bytes

Table of Contents

Python

Unexpected and surprising


Default variables and references

def foo(a=[]):
    a.append(5)
    return a

print(foo())
print(foo())

Expected result: [5] and [5]

Result: [5] and [5,5]

Reason: default arguments are passed by reference, so if changed, the default argument also changes.

Tested with: Python 2.7, Python 3.6