Skip to content

Instantly share code, notes, and snippets.

@IuryAlves
Last active October 20, 2015 22:14
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 IuryAlves/117611f2da0b2bf054d6 to your computer and use it in GitHub Desktop.
Save IuryAlves/117611f2da0b2bf054d6 to your computer and use it in GitHub Desktop.
# 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