Last active
October 20, 2015 22:14
-
-
Save IuryAlves/117611f2da0b2bf054d6 to your computer and use it in GitHub Desktop.
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
# raw types are immutable variables | |
x = 1 | |
def change(x): | |
x = 2 | |
change(x) | |
assert x == 1 | |
# you can use the global statement, but it's not a good practice | |
def change(): | |
global x | |
x = 2 | |
change() | |
assert x == 2 | |
# you can return the value and assign to the variable, it's better than global statement | |
def change(x): | |
x = 2 | |
return x | |
x = change(x) | |
assert x == 2 | |
# Immutable variables | |
# lists, sets, dicts and collections in general are mutable vars | |
itens = ["sword", "shield"] | |
def change(itens): | |
itens.append("bow") | |
change(itens) | |
assert itens == ["sword", "shield", "bow"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment