0

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

2
  • But its giving me everything that I dont even need , isnt there a way to print just the names? Commented Nov 24, 2015 at 23:41
  • dir() returns a list of names in the current scope. Since everything is an object in Python, there isn't a difference (to dir) between an imported module and a variable holding a string. Commented Nov 24, 2015 at 23:52

1 Answer 1

1

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}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.