In [2]:
'''
LEGB
Local, Enclosing, Global, Built-in
'''
#The scopes are listed below in terms of hierarchy(highest to lowest/narrowest to broadest):

#Local(L): Defined inside function/class
#Enclosed(E): Defined inside enclosing functions(Nested function concept)
#Global(G): Defined at the uppermost level
#Built-in(B): Reserved names in Python builtin modules
Out[2]:
'\nLEGB\nLocal, Enclosing, Global, Built-in\n'
In [3]:
import builtins
In [8]:
#Local and Global Scope
x='global x'
def test():
    y='local y'
    print(y)
    print(x)
test()
#when we print y it checks the local scope and as it was there in local scope it printed the value, same goes for x but x was not in local scope, so python checks it in first enclosing scope [which is not there] and then in global scope and print the global value.
local y
global x
In [9]:
#y variable is defined inside a function so it has a local scope even we have called the function test but we can't access outside it's scope.
x='global x'
def test():
    y='local y'
    print(y)
test()
print(y)
local y
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-334f0e00280a> in <module>
      4     print(y)
      5 test()
----> 6 print(y)

NameError: name 'y' is not defined
In [11]:
#As x variable is defined globally, we can access it from inside the function or outside the  function.
x='global x'
def test():
    y='local y'
    print(y)
test()
print(x)
local y
global x
In [12]:
#you may be thinking that x variable is overwritten in the function but that's not the case.. when x is called function checks whether it exist locally, as it does it didn't need to check globally.
x='global x'
def test():
    x='local x'
    print(x)
test()
print(x)
local x
global x
In [14]:
#Setting global value from inside the function
x='global x'
def test():
    global x
    x='local x'
    print(x)
test()
print(x)
local x
local x
In [15]:
#you don't need x to be defined outside function to call it.
def test():
    global x
    x='local x'
    print(x)
test()
print(x)
local x
local x
In [17]:
def test(z):
    #global x
    x='local x'
    print(z)
test('local z')
print(z)
#because z is local to test function.
local z
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-a1759f9c1636> in <module>
      4     print(z)
      5 test('local z')
----> 6 print(z)

NameError: name 'z' is not defined
In [22]:
#BUILT-INS
#we are able to use min as it is a built-in function in python
m=min([5,4,2,1,3])
print(m)
1
In [21]:
#dir gives attributes of given object
print(dir(builtins))
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '__IPYTHON__', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'display', 'divmod', 'enumerate', 'eval', 'exec', 'filter', 'float', 'format', 'frozenset', 'get_ipython', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
In [23]:
#you can override the built-ins
def min():
    pass
m=min([5,4,2,1,3])
print(m)
#when we ran the min function, python found out the min function in global scope before checking the built-in scope.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-89527db8e63c> in <module>
      2 def min():
      3     pass
----> 4 m=min([5,4,2,1,3])
      5 print(m)

TypeError: min() takes 0 positional arguments but 1 was given
In [25]:
#Enclosing Scope
def outer():
    x = 'outer x' #local to outer function
    def inner():
        x='inner x' #local to inner function
        print(x)
    inner()
    print(x)
outer()
inner x
outer x
In [26]:
#first inner function will check if it has any local variable, if not then it will look in enclosing scope. then in global then in builtin.. we have x variable in enclosing scope.
def outer():
    x = 'outer x' #enclosing to inner function, #local to outer
    def inner():
        #x='inner x'
        print(x)
    inner()
    print(x)
outer()
outer x
outer x
In [27]:
def outer():
    x = 'outer x' #enclosing to inner function, #local to outer
    def inner():
        nonlocal x #this means we are affecting the enclosing variable of inner function.
        x='inner x'
        print(x)
    inner()
    print(x)
outer()
inner x
inner x
In [28]:
x='global x'
def outer():
    #x = 'outer x' 
    def inner():
     #   x='inner x'
        print(x)
    inner()
    print(x)
outer()
print(x)
global x
global x
global x