I am using a .pop method on a Global list inside a function block, but the Global list is being updated outside the block. I thought that local variables can't modify Global variables.
This should not work, but it does:
import random
PhraseBank = ['a','b','c','d']
def getPuzzle(secretphrase):
phraseIndex = random.randint(0,len(PhraseBank)-1)
secretphrase = PhraseBank.pop(phraseIndex)
return secretphrase #Returns and item indexed from the PhraseBank
while len(PhraseBank) != 0:
secretphrase = getPuzzle(PhraseBank) #works
print(secretphrase, PhraseBank)
OUTPUT is:
a ['b', 'c', 'd']
d ['b', 'c']
c ['b']
b []
Why is PhraseBank getting updated Globally when I am only modifying it inside a function block?
PhraseBanklist is not copied when passed togetPuzzle(); names are local, but names are just references.