getting object instead of string from dir()

Kurt Smith kwmsmith at gmail.com
Wed Dec 17 16:59:18 EST 2008


On Wed, Dec 17, 2008 at 1:52 PM, Rominsky <john.rominsky at gmail.com> wrote:

> On Dec 17, 10:59 am, Christian Heimes <li... at cheimes.de> wrote:
> > Rominsky schrieb:
> >
> > > I am trying to use dir to generate a list of methods, variables, etc.
> > > I would like to be able to go through the list and seperate the
> > > objects by type using the type() command, but the dir command returns
> > > a list of strings.  When I ask for the type of an element, the answer
> > > is always string.  How do I point at the variables themselves.  A
> > > quick example is:
> >
> > > a = 5
> > > b = 2.0
> > > c = 'c'
> >
> > > lst = dir()
> >
> > > for el in lst:
> > >     print type(el)
> >
> > for name, obj in vars().iteritems():
> >     print name, obj
> >
> > Christian
>
> I do have some understanding of the pythonic methodology of
> programming, though by far I still don't consider myself an expert.
> The problem at hand is that I am coming from a matlab world and trying
> to drag my coworkers with me.  I have gotten a lot of them excited
> about using python for this work, but the biggest gripe everytime is
> they want their matlab ide.  I am trying to experiment with making
> similar pieces of the ide, in particular I am working on the workspace
> window which lists all the current variables in the namespace, along
> with their type, size, value, etc....  I am trying to create a python
> equivalent.  I can get dir to list all the variables names in a list
> of strings, but I am trying to get more info them.  hence the desire


Are you familiar with the ipython console?

http://ipython.scipy.org/moin/

It is quite powerful; in particular, the %who and %whos 'magic functions'
will do much of what you'd like:

[501]$ ipython
Python 2.5.2 (r252:60911, Jul 31 2008, 17:31:22)
Type "copyright", "credits" or "license" for more information.

IPython 0.8.1 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: a = 'foo'

In [2]: b = 'bar'

In [3]: c = 5.234

In [4]: import os

In [5]: d = os

In [6]: whos
Variable   Type      Data/Info
------------------------------
a          str       foo
b          str       bar
c          float     5.234
d          module    <module 'os' from '/usr/lib/python2.5/os.pyc'>
os         module    <module 'os' from '/usr/lib/python2.5/os.pyc'>

In [7]: import numpy as np

In [8]: aa = np.zeros(100)

In [9]: whos
Variable   Type       Data/Info
-------------------------------
a          str        foo
aa         ndarray    100: 100 elems, type `float64`, 800 bytes
b          str        bar
c          float      5.234
d          module     <module 'os' from '/usr/lib/python2.5/os.pyc'>
np         module     <module 'numpy' from
'/us<...>ages/numpy/__init__.pyc'>
os         module     <module 'os' from '/usr/lib/python2.5/os.pyc'>

And I trust you've heard of numpy, scipy and matplotlib?

http://www.scipy.org/

http://matplotlib.sourceforge.net/

 http://numpy.scipy.org/

Cheers,

Kurt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081217/a356c4d5/attachment-0001.html>


More information about the Python-list mailing list