[go: up one dir, main page]

0% found this document useful (0 votes)
42 views3 pages

Local Nonlocal Arguments

The document discusses the different types of variables in Python: global, local, and nonlocal. Global variables can be accessed anywhere in a program but cannot be modified inside a function without the global keyword. Local variables are only accessible within the block they are defined like a function. Nonlocal variables refer to variables in the nearest enclosing scope from a nested function, and the nonlocal keyword is used to modify them.

Uploaded by

ratheeshbr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

Local Nonlocal Arguments

The document discusses the different types of variables in Python: global, local, and nonlocal. Global variables can be accessed anywhere in a program but cannot be modified inside a function without the global keyword. Local variables are only accessible within the block they are defined like a function. Nonlocal variables refer to variables in the nearest enclosing scope from a nested function, and the nonlocal keyword is used to modify them.

Uploaded by

ratheeshbr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Global, Local and Nonlocal

variables
BY ALBERTO POWERS · APRIL 15, 2019
The scope of a variable plays an important role in any programming language.
Variables in Python are categorized into three categories viz. global, local and
non-local variables in Python programming.

What are the Python Global


Variables?
The variables that are declared outside of functions are global variables. They
can be accessed from anywhere of the program code, either inside or outside
of a function. Let us take a look at an example to be more clear on how global
variables are created and accessed.
1 x = 20


4 def my_func():
5     print("Value inside function:", x)


8 my_func()
9 print("Value outside function:", x)
Value inside function: 20
Value outside function: 20

How to change Global Variable From


Inside a Function?
Global variables can be accessed directly inside a function body, but it cannot
be modified inside a function body. To do so, global keyword is used.
1 x = 20
2  
3  
4 def my_func():
5     global x
6     x = 10
7     print("Value inside function:", x)
8  
9  
10 my_func()
11 print("Value outside function:", x)
Value inside function: 10
Value outside function: 10

What are the Python Local Variables?


A local variable is accessible inside a block of code like loop or functions and
cannot be accessed outside the blocks.
1 def foo():
2     y = 10
3     print(y)

5 foo()
The above program print 10 in the screen. Here y is the local variable. We
cannot access y outside of the function definition.

What are the Python Non-Local


Variables?
The nonlocal variable is used in a nested function whose local scope is not
defined. The nonlocal statement causes the listed identifiers to refer to
previously bound variables in the nearest enclosing scope excluding globals.
It takes the one “closest” to the point of reference in the source code. This is
also called “Lexical Scoping”.
1 def outer():
2     x = 10
3  
4     def inner():
5         nonlocal x
6         x = 20
7         print("inner:", x)
8  
9     inner()
10     print("outer:", x)
11  
12  
13 outer()
The output of the above program is:-
inner: 20
outer: 20

You might also like