Common mistakes we make in Python and why we should avoid that part 3

def mutable_test_function(first, second=[]):
second.extend(first)
return second
mutable_test_function([1,2,3], [1,2,3])
Out[63]: [1, 2, 3, 1, 2, 3]
In [64]: mutable_test_function([1,2,3])
Out[64]: [1, 2, 3]
In [65]: mutable_test_function([1,2,3])
Out[65]: [1, 2, 3, 1, 2, 3]
In [66]: mutable_test_function([1,2,3])
Out[66]: [1, 2, 3, 1, 2, 3, 1, 2, 3]
def mutable_test_function(first, second=None): 
if second is None:
second = []
second.extend(first)
return second
In [74]: mutable_test_function([1,2,3])
Out[74]: [1, 2, 3]
In [75]: mutable_test_function([1,2,3])
Out[75]: [1, 2, 3]
In [76]: mutable_test_function([1,2,3])
Out[76]: [1, 2, 3]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store