List of declared variables in interactive Python session?

Bengt Richter bokr at oz.net
Sun Jul 20 19:44:00 EDT 2003


On Sun, 20 Jul 2003 15:18:19 -0400, Peter Hansen <peter at engcorp.com> wrote:

>"Raymond Arthur St. Marie II of III" wrote:
>> 
>> >>> vars( )
>> 
>> {'d': 'pywin', '__builtins__': <module '__builtin__' (built-in)>, '__name__':
>> '__main__', 'pywin': <module 'pywin' from
>> 'C:\PYTHON22\lib\site-packages\Pythonwin\pywin\__init__.pyc'>, '__doc__': None}
>> 
>> >>> for v in vars(): print v
>> ...
>> 
>> Traceback (most recent call last):
>>   File "<interactive input>", line 1, in ?
>> RuntimeError: dictionary changed size during iteration
>
>for v in vars().copy(): print v
>
>works fine...
>
I sometimes find it handy to leave out underscore items:

 >>> [name for name in dir() if not name.startswith('_')]
 []
 >>> x=123
 >>> [name for name in dir() if not name.startswith('_')]
 ['name', 'x']

vs.

 >>> dir()
 ['__builtins__', '__doc__', '__name__', 'name', 'x']


Regards,
Bengt Richter




More information about the Python-list mailing list