getting object instead of string from dir()

rdmurray at bitdance.com rdmurray at bitdance.com
Wed Dec 17 21:36:57 EST 2008


Quoth Rominsky <john.rominsky at gmail.com>:
> vars seems to give an identical response as locals and globals, at
> least in my test name space.  All three are new commands for me.  I

Without arguments vars() returns the same thing as locals().

> like the idea of adopting either vars or locals instead of dir as it
> sets up the value for me to use.  I will play with them both a little
> more and read up on them to learn there uses and limitations.  The key
> step for me is still to be able to automatically query the key names
> from vars or locals for what type the variable is.  In my previous
> post I discussed using eval and the string in the key name such as
> 
> eval('type(%s)'%vars().keys()[0])

    >>> eval('type(%s)'%vars().keys()[0])
    <type 'module'>
    >>> vars().keys()[0]
    '__builtins__'
    >>> vars().values()[0]
    <module '__builtin__' (built-in)>
    >>> type(vars().values()[0])
    <type 'module'>
    >>> for name, obj in vars().items():
    ...     print "%s (%s)" % (name, type(obj))
    ... 
    __builtins__ (<type 'module'>)
    __name__ (<type 'str'>)
    __doc__ (<type 'NoneType'>)
    __package__ (<type 'NoneType'>)




More information about the Python-list mailing list