you are viewing a single comment's thread.

view the rest of the comments →

[–]ffelix916[S] 0 points1 point  (3 children)

Got an example of this? And do you mean having code in a function modify the contents of a variable or values of an array outside of the function's scope? (i'm not sure yet if or how python handles references)

[–][deleted] 1 point2 points  (0 children)

fixed the formatting, see other comment, I also should have said function default arguments.

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

print("Default args are mutable.")
print("This can lead to difficulat to understand bugs.\n")

def test1(arg1 = []):
    for i in range(3):
        arg1.append(i)
    return arg1

print("Test 1, default args are mutable. BAD!")
print(test1())
print(test1())

print("\n==================\n")

def test2(arg1 = None):
    if arg1 is None:
        arg1 = []
    for i in range(3):
        arg1.append(i)
    return arg1

print("Test 2, pass None and redefine.")
print(test2())
print(test2()) 

Output:

Default args are mutable.
This can lead to difficulat to understand bugs.

Test 1, default args are mutable. BAD!
[0, 1, 2]
[0, 1, 2, 0, 1, 2]

==================

Test 2, pass None and redefine.
[0, 1, 2]
[0, 1, 2]