Skip to content

Instantly share code, notes, and snippets.

@demacdolincoln
Created March 8, 2018 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demacdolincoln/98beafa250ecfe5be77b4e692d11c63c to your computer and use it in GitHub Desktop.
Save demacdolincoln/98beafa250ecfe5be77b4e692d11c63c to your computer and use it in GitHub Desktop.
simples exemplos da passagem por referência em python
l1 = [1,2]
l2 = l1
l1.append(3)
l2
def teste(lista):
for i in range(4, 10):
lista.append(i)
teste(l1)
l2
l1 == l2
id(l1) == id(l2)
import copy
l1 = [1, 2]
l2 = copy.copy(l1)
id(l1) == id(l2)
l1.append(3)
l2
l1
l1 = [[1,2,3], [4,5,6]]
l2 = copy(l1)
id(l1) == id(l2)
id[l1[0]] == id(l2[0])
l1[0].append(99)
l2
l1 = [[1,2,3], [4,5,6]]
l2 = copy.deepcopy(l1)
id(l1) == id(l2)
id(l1[0]) == id(l2[0])
l1[0].append(99)
l2
l1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment