locals() and globals() in Python

Python globals() function

Method globals() return a dictionary which contains global environment variables used by interpretor for the program.

Example: 

print(globals())


#output

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000024C64E14850>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\Stefan\\builtin1\\main.py', '__cached__': None}



Or writing each key:value of dictionary in a single line, example:

d_globals = globals()

for key in d_globals.copy():

    print(f"{key}: {d_globals[key]}")

#output

__name__: __main__

__doc__: None

__package__: None

__loader__: <_frozen_importlib_external.SourceFileLoader object at 0x000001FFF0B44850>

__spec__: None

__annotations__: {}

__builtins__: <module 'builtins' (built-in)>

__file__: C:\Users\Stefan\builtin1\main.py

__cached__: None



Python locals() function

Method locals(), return a dictionary which contains all environment variables (key: value) related to program local scope. 

Example:

print(locals())


#output

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001449C894850>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\Stefan\\builtin1\\main.py', '__cached__': None}


Or writing each key:value of dictionary in a single line, example:

d_locals = locals()

for key in d_locals.copy():

    print(f"{key}: {d_locals[key]}")


#output

__name__: __main__

__doc__: None

__package__: None

__loader__: <_frozen_importlib_external.SourceFileLoader object at 0x000001EFDCAF4850>

__spec__: None

__annotations__: {}

__builtins__: <module 'builtins' (built-in)>

__file__: C:\Users\Stefan\builtin1\main.py

__cached__: None


If we define a local variable in the program, it will appear in locals() output.

Example in below with variable named month:

def aboutyear():

    month = 'May'

    print(locals())

    print(locals()['month'])


print(aboutyear())


#output: 

{'month': 'May'}

May

We see in this case printing locals() inside function aboutyear will print only one key:value  for the variable month defined inside (or local) in the function itself.


We can change values for a key from locals() or globals(). Below is an example of doing this with a key from globals():

year=2023

def aboutmonth():

    month = 'May'


print(globals())

globals()['year']=2022

print(globals())


#output: 

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000023389FB4850>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\Stefan\\builtin1\\main.py', '__cached__': None, 'year': 2023, 'aboutmonth': <function aboutmonth at 0x0000023389EF3E20>}


{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000023389FB4850>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\Stefan\\builtin1\\main.py', '__cached__': None, 'year': 2022, 'aboutmonth': <function aboutmonth at 0x0000023389EF3E20>}


We see first print(globals()) will contain global variable year with value 2023, but it will not print local variable month which is inside function aboutmonth

Then we change global variable year to 2022 using globals()['year']=2022

Hence second print(globals()) will contain global variable year with value 2022