'Out': {},
'_': '',
'__': '',
'___': '',
'__builtin__': ,
'__builtin__': ,
'__builtins__': ,
'__builtins__': ,
'__name__': '__main__',
'_dh': ['/tmp/tmp98r7zamj'],
'_i': '',
'_i1': 'locals()',
'_ih': ['', 'locals()'],
'_ii': '',
'_iii': '',
'_oh': {},
'_sh': ,
'_sh': ,
'exit': ,
'get_ipython': >,
'quit': }
def localsNotPresent():
return locals()
def localsPresent():
present = True
return locals()
print('localsNotPresent:', localsNotPresent())
print('localsPresent:', localsPresent())
Running the program results in:
localsNotPresent: {}
localsPresent: {'present': True}
def localsPresent():
present = True
print(present)
locals()['present'] = False;
print(present)
localsPresent()
Running the program results in:
True
True
Unlike the globals dictionary that can change the value of a variable, locals do not change information inside the local namespace.
Previous post: The globals () function in Python
Next lesson: List () function in Python