Python Scope


Only within the created region is a variable available. This is known as scope.

Python has two types of scopes, which is :

  • Local Scope
  • Global Scope
You can also search for these topics, python scope in future, scope rules in python, python assignment scope, how to execute conditional using python, how to create scopes in python, python check the scope, default value of python scope, example for python scope, Example for python scope.

Local Scope

A variable generated within a function inside is called as function's local scope and is only available in that function.

Example :- In this function a variable generated inside a function is available :

def myfunc():
  x = 300
  print(x)

myfunc()

# It gives an error
# print(x)

Output :-

300

You can also search for these topics, python local scope example, local scope keyword in python, python key value store, package values to the scope of python requirements, python working of local scope, ways to create the python scope.

Function Inside Function

The variable x is not accesible outside of the function as described in the example above but is usable in any function inside the function :

Example :- A function within the function allows access to the local variable :

def myfunc():
  x = 300
  def myinnerfunc():
    print(x)
  myinnerfunc()

myfunc()

Output :-

300

You can also search for these topics, python function inside function, Example for inside function in python, python access with in the function, function outside the function in python.

Global Scope

A variable created in the outside of a function (python main body) is a global variable and is part of the global scope.

Global variables are available within any global or local scope.

Example :- An outside function produced variable is global and can be used :

x = 300

def myfunc():
  print(x)

myfunc()

print(x)

Output :-

300
300

You can also search for these topics, python global scope variable, global keyword scope in python, property value of python global scope, how to update global scope in python, variables in python global, Example for python global scope.

Naming Variables

If within and outside one function you are operating with the same variable name, Python will treat them as two independent variables, one in the global scope (except the function) and the other in the local scope (within the function) :

Example :- It print the local x, then the global x is printed by the code :

x = 300

def myfunc():
  x = 200
  print(x)

myfunc()

print(x)

Output:-

200
300

You can also search for these topics, python naming variables, same variable name in python, python using two naming variables, same variable name inside and outside of a function using python, naming variables in python global scope, example for python scope naming variable.

Global Keyword

You can use the global keyword if you need to build a global variable but are in the local field.

The variable is global with the global keyword.

Example 1 :- The variable is the global scope if you use the global keyword :

def myfunc():
  global x
  x = 300

myfunc()

print(x)

Output :-

300

Use the global keyword to transform to a global variable within a function.

Example 2 :- To modify the value of a global variable within a function, use the global keyword to refer to the variable :

x = 300

def myfunc():
  global x
  x = 200

myfunc()

print(x)

Output :-

200

You can also search for these topics, python global keyword not working, keywords in global keywords best practices, example for global keywords in python, python documentation global keyword, global keyword invalid syntax with python, python list in function global keyword, Example for python global keyword.