Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Listing variables

Reply
Thread Tools

Listing variables

 
 
vsoler
Guest
Posts: n/a
 
      10-25-2009
Say that a have:

# file test.py
a=7


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?

Vicente Soler
 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      10-25-2009
> Say that a have:
>
> # file test.py
> a=7
>
>
> 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?


dir(test)

works for any scope you want (except in some C modules...was
peeved at mod_python for this reason when I was playing with it a
while back). I use this for debugging all the time:

dir(foo.bar.whatever)

or if I want to remember some less-used method on a string/list/dict:

dir("")
dir([])
dir({})

-tkc


 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      10-25-2009
vsoler wrote:
> Say that a have:
>
> # file test.py
> a=7
>
>
> 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?
>
> Vicente Soler
>
>

dir(test)


 
Reply With Quote
 
vsoler
Guest
Posts: n/a
 
      10-25-2009
On Oct 25, 12:01*pm, Tim Chase <(E-Mail Removed)> wrote:
> > Say that a have:

>
> > # file test.py
> > a=7

>
> > 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?

>
> * *dir(test)
>
> works for any scope you want (except in some C modules...was
> peeved at mod_python for this reason when I was playing with it a
> while back). *I use this for debugging all the time:
>
> * *dir(foo.bar.whatever)
>
> or if I want to remember some less-used method on a string/list/dict:
>
> * *dir("")
> * *dir([])
> * *dir({})
>
> -tkc


Tim,

If I just input dir(test) I don't get "a" in my list.

>>> import test
>>> dir(test)

['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'__path__']
>>>


I am using python 2.6

Am I doing anything wrong?
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      10-25-2009
> If I just input dir(test) I don't get "a" in my list.
>
>>>> import test
>>>> dir(test)

> ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
> '__path__']
>
> I am using python 2.6
>
> Am I doing anything wrong?



Are you importing the module you think you are?

tim@rubbish:~/tmp$ echo "a=42" > test.py
tim@rubbish:~/tmp$ python2.5
>>> import test
>>> dir(test)

['__builtins__', '__doc__', '__file__', '__name__', 'a']


Granted this is 2.5 (the most current I have on my Debian box,
but I also tested in 2.3 and 2.4 which are also installed)
instead of 2.6 but they should all behave the same. If I remove
test.py/test.pyc, I get the following:

tim@rubbish:~/tmp$ rm test.py test.pyc
tim@rubbish:~/tmp$ python2.5
>>> import test
>>> dir(test)

['__builtins__', '__doc__', '__file__', '__name__', '__path__']
>>> test.__file__

'/usr/lib/python2.5/test/__init__.pyc'

because there's apparently a module named "test" in the standard
distribution that gets found instead.

-tkc



 
Reply With Quote
 
vsoler
Guest
Posts: n/a
 
      10-25-2009
On Oct 25, 1:32*pm, Tim Chase <(E-Mail Removed)> wrote:
> > If I just input dir(test) I don't get "a" in my list.

>
> >>>> import test
> >>>> dir(test)

> > ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
> > '__path__']

>
> > I am using python 2.6

>
> > Am I doing anything wrong?

>
> Are you importing the module you think you are?
>
> tim@rubbish:~/tmp$ echo "a=42" > test.py
> tim@rubbish:~/tmp$ python2.5
> *>>> import test
> *>>> dir(test)
> ['__builtins__', '__doc__', '__file__', '__name__', 'a']
>
> Granted this is 2.5 (the most current I have on my Debian box,
> but I also tested in 2.3 and 2.4 which are also installed)
> instead of 2.6 but they should all behave the same. *If I remove
> test.py/test.pyc, I get the following:
>
> tim@rubbish:~/tmp$ rm test.py test.pyc
> tim@rubbish:~/tmp$ python2.5
> *>>> import test
> *>>> dir(test)
> ['__builtins__', '__doc__', '__file__', '__name__', '__path__']
> *>>> test.__file__
> '/usr/lib/python2.5/test/__init__.pyc'
>
> because there's apparently a module named "test" in the standard
> distribution that gets found instead.
>
> -tkc


Tim,

You were right. When I renamed my test.py file into test77.py it
worked perfectly well. Thank you.

Is there a way to know which test.py it was importing?
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      10-25-2009
>> tim@rubbish:~/tmp$ rm test.py test.pyc
>> tim@rubbish:~/tmp$ python2.5
>> >>> import test
>> >>> dir(test)

>> ['__builtins__', '__doc__', '__file__', '__name__', '__path__']
>> >>> test.__file__

>> '/usr/lib/python2.5/test/__init__.pyc'
>>
>> because there's apparently a module named "test" in the standard
>> distribution that gets found instead.

>
> You were right. When I renamed my test.py file into test77.py it
> worked perfectly well. Thank you.
>
> Is there a way to know which test.py it was importing?


well, as my simple code showed, you can check test.__file__ or
test.__path__ if you're curious. Python just searches through
your $PYTHONPATH which you can determine at runtime via sys.path

-tkc


 
Reply With Quote
 
vsoler
Guest
Posts: n/a
 
      10-25-2009
On Oct 25, 5:07*pm, Tim Chase <(E-Mail Removed)> wrote:
> >> tim@rubbish:~/tmp$ rm test.py test.pyc
> >> tim@rubbish:~/tmp$ python2.5
> >> *>>> import test
> >> *>>> dir(test)
> >> ['__builtins__', '__doc__', '__file__', '__name__', '__path__']
> >> *>>> test.__file__
> >> '/usr/lib/python2.5/test/__init__.pyc'

>
> >> because there's apparently a module named "test" in the standard
> >> distribution that gets found instead.

>
> > You were right. When I renamed my test.py file into test77.py it
> > worked perfectly well. Thank you.

>
> > Is there a way to know which test.py it was importing?

>
> well, as my simple code showed, you can check test.__file__ or
> test.__path__ if you're curious. *Python just searches through
> your $PYTHONPATH which you can determine at runtime via sys.path
>
> -tkc


Thank you Tim, everything is clear now.

Vicente Soler
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
listing out available variables in an ERB template mgerstenblatt@gmail.com Ruby 1 09-27-2010 03:51 AM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
listing all property variables of a class instance =?iso-8859-1?B?QW5kcuk=?= Python 5 06-25-2007 09:14 PM
Listing all variables of a source code rusttree@gmail.com C++ 4 06-23-2005 02:12 PM
Listing of declared variables and functions ohms377 Python 3 05-11-2005 11:08 AM



Advertisments