Local And Global Scope

Local Scope #

Variables that are assigned inside a function have a local scope to that function.

These are called local variables.

Global Scope #

Variables that have a global scope are (typically) defined outside of functions.

These are called global variables.

Global Statements #

Global variables can be modified in a function with a global statement .

Why do scopes matter? #

  • Code in global scope cannot use local variables
  • Code in local scope can access global variables
  • Code in a function’s local scope cannot use variables in any other local scope
  • You can use the same name for different variables if they are in different scopes.
    • It’s not considered best practice to do this though, so this should be avoided unless for a good reason

How to tell if a variable is global or local? #

Four rules:

  • If a variable is defined in a global scope its global
  • If a global statement is used in a function its global
  • If a variable is used in an assignment statement in a function it’s local
  • But if a variable is not used in an assignment statement it is a global variable.

See: variables