Python global Keyword
Allows the conversion of a global read-only variable into a writeable variable.
Variables defined outside of a function or class are global but read-only, and are known as "constants". The global keyword tells Python that the variable is now read-writeable - it is not being treated as a constant.
myConstant = 10 # This is a global read-only variable (a "constant") class MyGlobalChanger def modifyMyConstant(): global myConstant # This allows us to change the "constant" value myConstant = myConstant + 5 print("myConstant is ", myConstant) changer = MyGlobalChanger() changer.modifyMyConstant() print("myConstant is now ", myConstant)
Notes: Use with caution! Global variables usually should never be changed because this can cause side-effects when other code expects the global variable to behave like a constant.