[Tutor] question about listing variables defined since session started

Steven D'Aprano steve at pearwood.info
Tue May 1 01:44:59 CEST 2012


Comer Duncan wrote:
> Hi,
> 
> I have a newbie type question.  Say I have started a python (or
> ipython) session and have done some imports and have also defined some
> new variables since the session started.  So, I have in my current
> namespace a bunch of things. Suppose I  want to list just those
> variable  names which have been defined since the session started but
> not include the names of the objects that who and whos will return.

What's "who and whos"?


> How to do that?  In matlab, this is what the who returns, but in
> python I seem to always get a raft of things since I typically do
> import a bunch of things.

That depends on what you are doing.

If you are using dir(), then you will get a list of all the currently existing 
objects in your session. There's no way to show only "names defined since the 
session started". Maybe iPython does something like that, but I doubt it.

Taken literally, I don't think you want is possible in Python. When objects 
are created, they aren't timestamped with the moment of when they were 
created, or who created them, or anything else. So there's no way to tell the 
difference between "x = 1" done during system startup and "x = 1" done after 
system startup.

But why do you care? If you explain in more detail what you are hoping to 
accomplish, perhaps we can think of an alternative way to do so.


-- 
Steven



More information about the Tutor mailing list