COMP1531

2.3 - Python - Globals

 

Global Variables

  • All variables in the python global scope can be accessed (read) in any other scope in the program.
  • To modify a global variable you must use the "global" keyword at the top of the function using it.

global.py

myVariable = 5

def printSomething():
  print(myVariable)
  
def writeSomethingBad():
  myVariable = 6
  
def writeSomethingGood():
  global myVariable
  myVariable = 6

Global Variables

  • How will we use this information to make iteration 1 easier?
Made with Slides.com