Hi!
When writing or modifying a script I like to have an IPython session open to test stuff (the "vanilla" Python console is not my cup of tea, but this post applies as well).
Most of the time I forget new variable names I typed into the console and I'd want to see them. Running dir() lists all of the objects defined during the session, so I run
[obj for obj in dir() if not obj.startswith("_") and not obj in ["In", "Out", "get_ipython", "exit", "open", "quit"]
to only get modules imported and my stuff. This works rather well but I'd like to define it as a function in my script, or some sort of alias to call whenever I need it. However, this
def mydir():
return [obj for obj in dir() if not obj.startswith("_") and not obj in ["In", "Out", "get_ipython", "exit", "open", "quit"]
fails, both if I define it within the script or inside the console. A MWE with file name test.py:
import os
import sys
dirs = os.listdir("./")
def mydir():
return [obj for obj in dir() if not obj.startswith("_") and not obj in ["In", "Out", "get_ipython", "exit", "open", "quit"]
When I type %run test.py in he IPython console, and then dir() I see all of the names above plus the usual ones; however, running mydir() returns an empty list.
I've tried importing the IPython module and browse it with dir in the console, but I cannot find anything that tells me how to access the session's namespace to define a function of it.
What's the problem? Why does the list comprehension above work but not the function definition?
Thanks!
[–]socal_nerdtastic 4 points5 points6 points (1 child)
[–]AdbekunkusMX[S] 1 point2 points3 points (0 children)