Created
March 8, 2018 17:15
-
-
Save demacdolincoln/98beafa250ecfe5be77b4e692d11c63c to your computer and use it in GitHub Desktop.
simples exemplos da passagem por referência em python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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