Reading Time: 1 minutes
Using globals(), locals() & vars() in Python
globals()
The builtin globals() function returns a dictionary containing objects accessible anywhere in the program.
>>> help(globals) Help on built-in function globals in module builtins: globals(...) globals() -> dictionary Return the dictionary containing the current scope's global variables. ### EXAMPLES ### >>> aGlobalVariable = 'accessible everywhere.' >>> print(globals()) {'__builtins__': <module 'builtins' (built-in)>, '__spec__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, 'aGlobalVariable': 'accessible everywhere.', '__name__': '__main__', '__doc__': None} >>> def aConfinedPieceOfCode(): aLocalVariable = 'accessible only locally.' print(globals()) >>> aConfinedPieceOfCode() {'__builtins__': <module 'builtins' (built-in)>, '__spec__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, 'aConfinedPieceOfCode': <function aConfinedPieceOfCode at 0x03159A08>, 'aGlobalVariable': 'accessible everywhere.', '__name__': '__main__', '__doc__': None} >>> 'aGlobalVariable' in globals() True
locals()
The builtin locals() function returns a dictionary containing objects accessible only in the current scope.
>>> help(locals) Help on built-in function locals in module builtins: locals(...) locals() -> dictionary Update and return a dictionary containing the current scope's local variables. ### EXAMPLES ### >>> aLocalVariable = 'accessible only locally.' >>> print(locals()) # locals() of module {'__builtins__': <module 'builtins' (built-in)>, '__spec__': None, '__package__': None, '__doc__': None, 'aLocalVariable': 'accessible only locally.', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__name__': '__main__'} >>> def aConfinedPieceOfCode(): anotherLocalVariable = 'accessible only locally; present inside a function.' print("Printing dictionary of local objects:", locals()) # locals() of this scope. if 'anotherLocalVariable' in locals(): print("Present.") >>> aConfinedPieceOfCode() Printing dictionary of local objects: {'anotherLocalVariable': 'accessible only locally; present inside a function.'} Present.
Bear in mind that any top-level code in the module(i.e. code at the first indent level) goes in the local namespace of the module, constituting the global namespace.
vars()
The builtin function vars(obj) returns the dictionary of the attributes belonging to an instance/object as returned by the __dict__ attribute of the object. When used without an argument, it returns the the dictionary of local objects.
>>> help(vars) Help on built-in function vars in module builtins: vars(...) vars([object]) -> dictionary Without arguments, equivalent to locals(). With an argument, equivalent to object.__dict__. ### EXAMPLES ### >>> class Ethan(): def __init__(self): self.name = 'Ethan' self.age = 23 >>> ethan = Ethan() # Equivalent to object.__dict__ when called by passing an object. >>> vars(ethan) {'age': 23, 'name': 'Ethan'} >>> ethan.__dict__ {'age': 23, 'name': 'Ethan'} >>> 'age' in vars(ethan) True # Equivalent to locals() when called without arguments. >>> vars() {'__loader__': <class '_frozen_importlib.BuiltinImporter'>, 'Ethan': <class '__main__.Ethan'>, '__spec__': None, '__package__': None, '__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__', '__doc__': None, 'ethan': <__main__.Ethan object at 0x03090810>}