List of declared variables in interactive Python session?

Raymond Arthur St. Marie II of III rastm2 at aol.commorespam
Tue Jul 22 03:07:01 EDT 2003


Bengt Richter  informs Ray St.Marie that...

>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']     <<<_see Below
>
>vs.
>
 >>> dir()
> ['__builtins__', '__doc__', '__name__', 'name', 'x']
>
>
>Regards,
>Bengt Richter

Cool and now I do too ...you ... tip monster you !   

Thanks - (giggling with that "you b at stard" grin on my face) 
(Why is it the simplest things make ya laugh.)

But this -  the true beauty in your example is that... 
You didn't enter an iterater 'name' into the dir ( ) 
before you displayed it. Nice trick.
In fact you never have to in a dir( ) call.
The only reason 'name'     <<<_see above
is in the dir( ) now is because
this is your second for loop. 
I mean to say that the dir( )
was displayed first the first time 
then 'name' was added --- am I right?
Now it's there for the second call. 

Yet in the examples that ...

<I've lost the original thread and 
don't have the 2 gentilmen's names that started it>

...that they were working on, it was pretty obvious that 
you had to have an iterater variable established first for the 
vars( ) call.

If I don't put the ' v '  in the dir( ) before 
the call to vars( ) first I get the Traceback
because I'm changing the size of the dictionary
>>> for d in dir( ): print d 
... __builtins__    # i Know i know ive learned nothing sorry
__doc__
__name__
name
pywin
>>> for v in vars( ): print v
... 
name                # okay  there's the first variable  and then

Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
RuntimeError: dictionary changed size during iteration
>>> 

And the point to the second gentilman in the thread was 
that his call to  

>>> for v in vars( ).copy( ): print v

only works because the call to copy( ) does this first just
like declaring the ' v ' in my example -- only it a one liner. :>)

When I was new at this and, dir( ) and vars( ) were something
I learned early on, It didn't seam that intuitive to me.

........and so i go on and on and on about something only
I could find intersting, and I shall never more on this subject
Sorry Y'all.

Ray St. Marie
Stating the obvious since birth... Whaaaa.




More information about the Python-list mailing list