Let see what python doc has to say:
In Python, variables that are only referenced inside a function are
implicitly global. If a
variable is assigned a value anywhere within the function’s body, it’s assumed
to be a local unless explicitly declared as global[R]
In the below example, we can see x is global variable, and is accessible inside the function.
Test
Cases
Set x inside the function.
The value of x within the
function is changed i.e 20
Accessing the value of x
outside the function.
The value of x outside
the function is printed as 10
This
clearly means the variable x inside and outside the functions are not same.
This
can be proved by their memory addresses.
Output:
267516128
267515968
The two different memory address can be seen for the same variable x.
This
proves that the variable x, is not same, they are different.
The first
one is global and the other one which is inside
function is local.
If we
want to access global variable inside the functions, then we
have to explicitly declare the variable as global .
Let’s revisit what python doc has to say:
In
Python, variables that are only referenced inside a function are implicitly
global. If a variable is assigned a value anywhere within the function’s body,
it’s assumed to be a local unless explicitly declared as global[R]
Understanding nonlocal variable
First understand the below code construct.
Output:
The
value of x in m1 20
The
value of id of x in m1 1453258976
The
value of x in m2 30
The
value of id of x in m2 1453259136
The
value of x in m1 20 #the changed value of x=30 is NOT reflected in m1
The
value of id of x in m1 1453258976
- There are two functions m1 and m2. Function m2 is subfunction of m1.
- Function m1 has a variable x having value 20.
- Function m2 has also same name variable x
having value 30.
- When m2 is called from m1, variable x is set to
30.
- When trying to access the value of x from m1,
the value of x is still 20.
- This means that change in value of x in m2 in
local to m2 and is not propogated to m1.
- What if we change the value of x
in m2, it should also reflect in m1.
- For this set the variable x as nonlocal in m2.
Output:
The
value of x in m1 20
The
value of id of x in m1 1453258976
The
value of x in m2 30
The
value of id of x in m2 1453259136
The
value of x in m1 30 #the changed value of x=30 is
reflected in m1
The
value of id of x in m1 1453259136
After declaring the variable x as nonlocal in m2, the changed
value of x=30 in m2 is reflected in m1.
Let see what python doc has to say:
In Python, variables that are only referenced inside a function are
implicitly global. If a
variable is assigned a value anywhere within the function’s body, it’s assumed
to be a local unless explicitly declared as global[R]
In the below example, we can see x is global variable, and is accessible inside the function.
In the below example, we can see x is global variable, and is accessible inside the function.
Test
Cases
Set x inside the function.
The value of x within the
function is changed i.e 20
Accessing the value of x outside the function.
Set x inside the function.
Accessing the value of x outside the function.
The value of x outside
the function is printed as 10
This
can be proved by their memory addresses.
Output:
267516128
267515968
The two different memory address can be seen for the same variable x.
This
proves that the variable x, is not same, they are different.
The first
one is global and the other one which is inside
function is local.
If we
want to access global variable inside the functions, then we
have to explicitly declare the variable as global .
Let’s revisit what python doc has to say:
In
Python, variables that are only referenced inside a function are implicitly
global. If a variable is assigned a value anywhere within the function’s body,
it’s assumed to be a local unless explicitly declared as global[R]
Understanding nonlocal variable
First understand the below code construct.
Output:
The
value of x in m1 20
The
value of id of x in m1 1453258976
The
value of x in m2 30
The
value of id of x in m2 1453259136
The
value of x in m1 20 #the changed value of x=30 is NOT reflected in m1
The
value of id of x in m1 1453258976
- There are two functions m1 and m2. Function m2 is subfunction of m1.
- Function m1 has a variable x having value 20.
- Function m2 has also same name variable x having value 30.
- When m2 is called from m1, variable x is set to 30.
- When trying to access the value of x from m1, the value of x is still 20.
- This means that change in value of x in m2 in local to m2 and is not propogated to m1.
- What if we change the value of x in m2, it should also reflect in m1.
- For this set the variable x as nonlocal in m2.
Output:
The
value of x in m1 20
The
value of id of x in m1 1453258976
The
value of x in m2 30
The
value of id of x in m2 1453259136
The
value of x in m1 30 #the changed value of x=30 is
reflected in m1
The
value of id of x in m1 1453259136
After declaring the variable x as nonlocal in m2, the changed
value of x=30 in m2 is reflected in m1.
No comments:
Post a Comment