Listing variables

Ben Finney ben+python at benfinney.id.au
Sun Oct 25 12:08:56 CET 2009


vsoler <vicente.soler at gmail.com> writes:

> At the prompt:
> import test
> dir()
>
> I would like to see the variables created in the test namespace.
> However, variable "a" does not appear in the list, only "test". Since
> I know that var "a" is reachable from the prompt by means of test.a,
> how can I list this sort of variables?

    >>> help(dir)
    Help on built-in function dir in module __builtin__:

    dir(...)
        dir([object]) -> list of strings
        
        Return an alphabetized list of names comprising (some of) the
        attributes
        of the given object, and of attributes reachable from it:
        
        No argument:  the names in the current scope.
        Module object:  the module attributes.
        […]

So ‘dir()’ returns the names in the current scope, which is why the
names in the module ‘test’ don't appear there.

-- 
 \          “What I have to do is see, at any rate, that I do not lend |
  `\      myself to the wrong which I condemn.” —Henry Thoreau, _Civil |
_o__)                                                    Disobedience_ |
Ben Finney



More information about the Python-list mailing list