Getting currently loaded variables
The function dir()
will list all loaded environment variables, such as:
a = 2
b = 3
c = 4
print(dir())
will return
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b', 'c']
Find below what the documentation of dir
says:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
Getting variable methods and attributes
You can also use dir()
to list the methods and attributes associated with an object, for that you shall use: dir(<name of object>)
Getting size of variables currently loaded
If you wish to evaluate the size of your loaded variables/objects you can use sys.getsizeof()
, as such:
sys.getsizef(a)
sys.getsizof(<name of variable>)
sys.getsizeof()
gets you the size of an object in bytes (see this post for more on it)
Wrapping up
You could combine this functionality in some sort of loop like such
import sys
a =2
b = 3
c = 4
d = 'John'
e = {'Name': 'Matt', 'Age': 32}
for var in dir():
print(var, type(eval(var)), eval(var), sys.getsizeof(eval(var)))
Hope that helps!