getting object instead of string from dir()

Andrew Nelis andrew.nelis at gmail.com
Wed Dec 17 12:26:13 EST 2008


On Dec 17, 5:16 pm, Rominsky <john.romin... at gmail.com> wrote:
> 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)
>
> Right now I am understandably getting all types being output as
> strings, how do i get the type of the actual objects returned from dir
> ()?

The builtin functions "locals" and "globals" will both return a
dictionary whose keys are the variable names and values are the items
corresponding to those keys;

>>> locals()['b']
2.0



More information about the Python-list mailing list