Re: List of declared variables in interactive Python session?

Discussion in 'Python' started by vincent wehren, Jul 19, 2003.

  1. "Fredrik Fornwall" <> schrieb im Newsbeitrag
    news:pNdSa.397$...
    > Hello.
    >
    > Is there a way to list all the declared variables in an interactive
    > Python session?


    use "vars() "

    HTH,

    Vincent Wehren

    >
    > Thanks,
    > Fredrik
    >
     
    vincent wehren, Jul 19, 2003
    #1
    1. Advertising

  2. On Sun, 20 Jul 2003 15:18:19 -0400, Peter Hansen <> 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
     
    Bengt Richter, Jul 21, 2003
    #2
    1. Advertising

  3. 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@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.
     
    Raymond Arthur St. Marie II of III, Jul 22, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. WJ
    Replies:
    2
    Views:
    493
  2. JohnZing

    declared or not declared ?

    JohnZing, Feb 5, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,595
    Jon Paal
    Feb 5, 2006
  3. Luke
    Replies:
    1
    Views:
    435
    Guest
    Sep 30, 2004
  4. S. M. Tuttle
    Replies:
    2
    Views:
    1,185
    Brian van den Broek
    Jul 21, 2005
  5. chowchho
    Replies:
    7
    Views:
    634
    Patrice
    Mar 28, 2008
Loading...

Share This Page