You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All variables declared inside the function body have scope of that function.
They do not pollute outside scope.
Arguments also live in function's scope.
defmyFuncA( num ):
result=num**numreturnresultprint( "myFuncA(3) => ", myFuncA( 3 ) )
# => myFuncA(3) => 27# print( num ) # NameError: name 'num' is not defined# print( result ) # NameError: name 'result' is not defined
Variables defined on file level scope are called as global variables.
variables defined inside the function body are called as local variables.
To override a global variable, we need to tell Python it is a global variable using global keyword.
fruit="Mango"drink="Vanilla Shake"defmyFuncD():
globalfruit# define fruit as globalfruit="Apple"# change its valuedrink="Virgin Mojito"# creates a local variableprint("myFuncD-scope: fruit => ", fruit)
print("myFuncD-scope: drink => ", drink)
myFuncD()
print("global-scope: fruit => ", fruit)
print("global-scope: drink => ", drink)
# => myFuncD-scope: fruit => Apple# => myFuncD-scope: drink => Virgin Mojito# => global-scope: fruit => Apple# => global-scope: fruit => Vanilla Shake# If a global variable is not defined, using `global` variable, it can be created.defmyFuncE():
globalcar# define fruit as global, but doesn't create onecar="Mercedes"# create global variable if not present and assign a valueprint("myFuncE-scope: car => ", car)
myFuncE()
print("global-scope: car => ", car)
# => myFuncE-scope: car => Mercedes# => global-scope: car => Mercedes# Inner python function can also use `global` keyworddefmyFuncF():
country="India"# creates a local variabledefmyInnerFuncF():
globalcountry# define country as global variablecountry='Denmark'# assign a value to global variableprint("myInnerFuncF-scope: country => ", country)
myInnerFuncF()
print("myFuncF-scope: country => ", country)
myFuncF()
print("global-scope: country => ", country)
# => myInnerFuncF-scope: country => Denmark# => myFuncF-scope: country => India# => global-scope: country => Denmark
Use of nolocal keyword
A new variable is always created inside an inner function if we assign a value to a variable even if the same variable is passed to the inner fuction lexically.
The global keyword is only useful to use variables from global scope.
To override variable from lexical scope, we need to use nolocal keyword.
However, unlike global keyword, nolocal does not create a variable in any of the outer scopes.
The nonlocal does not work for variables coming from global scope, have to use global keyword.
defmyFuncG():
color='red'taste='sweet'mood='happy'defmyInnerFuncG():
nonlocalcolor# comes from myFuncG-scopecolor='green'taste='sour'defmyNestedInnerFuncG():
nonlocalmood# comes from myFuncG-scopemood='sad'# color comes from myFuncG-scope as wellprint("myNestedInnerFuncG-scope: color, taste, mood => {}, {}, {}".format( color, taste, mood ))
myNestedInnerFuncG()
print("myInnerFuncG-scope: color, taste, mood => {}, {}, {}".format( color, taste, mood ))
myInnerFuncG()
print("myFuncG-scope: color, taste, mood => => {}, {}, {}".format( color, taste, mood ))
myFuncG()
# => myNestedInnerFuncG-scope: color, taste, mood => green, sour, sad# => myInnerFuncG-scope: color, taste, mood => green, sour, sad# => myFuncG-scope: color, taste, mood => => green, sweet, sad
Hoisting
# Like JavaScript, functions and variables are hoisted in Python.# However, hoisted variable are not assigned with an initial value.originalValue=2defmyFuncH():
originalValue=originalValue*2# multiply global value by 2 and create local variableprint( "myFuncH-scope: originalValue => ", originalValue )
myFuncH() # UnboundLocalError: local variable 'originalValue' referenced before assignment