getting object instead of string from dir()

Rominsky john.rominsky at gmail.com
Wed Dec 17 14:52:17 EST 2008


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
to do a type command on them.  I like the locals and globals commands,
but I am still trying to get more info.  I have started using the eval
command with the strings, which is working, but I am curious if there
is a better or more elegant way of getting the info.  The eval example
would be something like:

a = 5
b = 2.0
c = 'c'

lst = dir()

for el in lst:
   print el + '\t' + str(eval('type(%s)'%el))

It works, now I am curious if there is a better way.



More information about the Python-list mailing list