5
Be the code below:
def modif1(lista):
lista = [4,5,6]
lista = [1,2,3]
modif1(lista)
print(lista) #resultado: [1,2,3]
def modif2(lista):
lista[0] = 4
lista[1] = 5
lista[2] = 6
lista = [1,2,3]
modif2(lista)
print(lista) #resultado: [4,5,6]
def modif3(lista):
lista[:] = [4,5,6]
lista = [1,2,3]
modif3(lista)
print(lista) #resultado: [4,5,6]
def modif4(lista):
L = lista[:]
L = [4,5,6]
lista = [1,2,3]
modif4(lista)
print(lista) #resultado: [1,2,3]
The function modif1 does not change the list because the function’s Scope already has a variable with the list name.
The function modif2 modifies the list because it has no name variable list and accesses list (in global Scope).
In function 3 comes the unexpected: when do lista[:] I’m not making a clone of lista? Why then when modifying lista[:] i modify the lista original and not just the clone? If so, what changes modif4 of modif3?