I am a newbie python user, I just migrated from R. Is there a function in Python like View() or ls() in R that lets you see the variables stored in the memory
1 Answer
You can use locals() to see all the variables in the local scope or globals() to see everything in the global scope. To get exactly what you want:
>>> import re
>>> x = 5
>>> y = 7
>>> foo = object
>>> [x for x in locals().keys() if not x.startswith('__')]
['re', 'x', 'y', 'foo']
If you also need the values:
>>> {key: val for key, val in locals().items() if not key.startswith('__')}
{'y': 7, 're': <module 're' from '/Users/ian/venv/lib/python2.7/re.pyc'>, 'foo': <object object at 0x103b1d0e0>, 'x': 5}
dir()returns a list of names in the current scope. Since everything is an object in Python, there isn't a difference (todir) between an imported module and a variable holding a string.