0

I ran into a very strange bug where an argument being initialized to an empty list, is grabbing a different global list. Can someone explain why this is happening?

def append_letters(s,letters,history_local=list()): 
    s += letters
    # Record delete action for undo
    history_local.append((2,len(letters)))
    return s, history_local

history = [(2, 2), (1, 'cdefg')]
s = 'ab'
hist = history.pop()
s = append_letters(s,hist[1])
print(history)
# Expect - [(2,2)] - only the un-popped part of history
# Result - [(2,2),(2,5)] - history is modified by reference even though it is neither returned nor passed to append_letters

illan
  • 163
  • 1
  • 13

1 Answers1

0

The issue was being caused by append_letters returning two arguments - s, and history which were being interpreted incorrectly at a higher level.

illan
  • 163
  • 1
  • 13