Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: getting object instead of string from dir()

Reply
Thread Tools

Re: getting object instead of string from dir()

 
 
Jean-Paul Calderone
Guest
Posts: n/a
 
      12-17-2008
On Wed, 17 Dec 2008 11:52:17 -0800 (PST), Rominsky <(E-Mail Removed)> wrote:
>On Dec 17, 10:59*am, Christian Heimes <(E-Mail Removed)> 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.



What about this:

>> for name, obj in vars().iteritems():
>> print name, obj
>>
>> Christian


Jean-Paul
 
Reply With Quote
 
 
 
 
Rominsky
Guest
Posts: n/a
 
      12-18-2008
On Dec 17, 1:21*pm, Jean-Paul Calderone <(E-Mail Removed)> wrote:
> On Wed, 17 Dec 2008 11:52:17 -0800 (PST), Rominsky <(E-Mail Removed)> wrote:
> >On Dec 17, 10:59*am, Christian Heimes <(E-Mail Removed)> 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.

>
> What about this:
>
> >> for name, obj in vars().iteritems():
> >> * * print name, obj

>
> >> Christian

>
> Jean-Paul


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
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])

I definitely have enough to move forward. Thanks everyone. If anyone
has any other ideas I would still be interested in learning more.
Thanks again.
 
Reply With Quote
 
 
 
 
rdmurray@bitdance.com
Guest
Posts: n/a
 
      12-18-2008
Quoth Rominsky <(E-Mail Removed)>:
> 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'>)

 
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
FAQ 4.1 Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)? PerlFAQ Server Perl Misc 0 04-17-2011 04:00 AM
FAQ 4.1 Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)? PerlFAQ Server Perl Misc 0 01-16-2011 05:00 PM
getting object instead of string from dir() Rominsky Python 5 12-18-2008 03:34 AM
Why 'class spam(object)' instead of class spam(Object)' ? Sergio Correia Python 7 09-18-2007 02:07 AM
object to object -- embed instead code_wrong HTML 4 05-23-2006 10:05 PM



Advertisments