Is there a quick way to get the R-equivalent of ls () in Python?

I am new to Python and usually use R and regularly use ls() to get the vector of all objects in my current environment, is there something that does the same thing fast in Python?

+7
python r
source share
1 answer

You are probably looking for dir :

With no arguments, return the list of names to the current local scope. With an argument, try to return a list of valid attributes for this object.

This may not be obvious at first, but when you are in a global scope (as is usually the case with the command line interpreter), the "current local scope" is a global scope (in this case __main__ ). That way, it will return all the variables and functions that you defined, all the imported modules, and a few things that are bound to each module or only to __main__ . For example:

 $ python3.3 >>> dir() ['__builtins__', '__doc__', '__loader__', '__name__', '__package__'] >>> import sys >>> i = 2+3 >>> dir() ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', 'i', 'sys'] 

This is always the same as sorted(locals().keys()) , but dir() much easier to type. And of course, it is perfectly parallel with dir(sys) to get the values โ€‹โ€‹defined by the sys module, dir(i) , to get the attributes of this integer 5 object, etc.

+13
source share

All Articles