getting object instead of string from dir()

Discussion in 'Python' started by Rominsky, Dec 17, 2008.

  1. Rominsky

    Rominsky Guest

    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
    ()?
     
    Rominsky, Dec 17, 2008
    #1
    1. Advertising

  2. Rominsky

    Andrew Nelis Guest

    On Dec 17, 5:16 pm, Rominsky <> 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
     
    Andrew Nelis, Dec 17, 2008
    #2
    1. Advertising

  3. Rominsky

    Rominsky Guest

    On Dec 17, 10:59 am, Christian Heimes <> 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.
     
    Rominsky, Dec 17, 2008
    #3
  4. On Wed, 17 Dec 2008 11:52:17 -0800, Rominsky wrote:

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



    Have you considered looking at existing IDEs instead of re-inventing the
    wheel? Python even comes with one, IDLE.



    --
    Steven
     
    Steven D'Aprano, Dec 18, 2008
    #4
  5. Rominsky

    Steve Holden Guest

    Steven D'Aprano wrote:
    > On Wed, 17 Dec 2008 11:52:17 -0800, Rominsky wrote:
    >
    >> 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.

    >
    >
    > Have you considered looking at existing IDEs instead of re-inventing the
    > wheel? Python even comes with one, IDLE.
    >
    >
    >

    I realise there are some very competent Python programmers whose primary
    environment is IDLE, but I'm afraid I always end up frustrated with it.

    I think Rominsky will learn a lot by noodling around in the way he
    proposes - he does, after all, confess he is experimenting, and that
    sounds to me like a great way to find out a lot in a fairly short time.

    Matlab is a very specific environment, and students have told me that
    there are pieces of its IDE that they really miss in Python, so this
    work may result in something that attracts more users to Python.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Dec 18, 2008
    #5
  6. Rominsky

    André Guest

    On Dec 17, 3:52 pm, Rominsky <> wrote:

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



    You might want to have a look at
    http://www.physics.ox.ac.uk/users/santoso/Software.WebLab.html

    André
     
    André, Dec 18, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UnVkeQ==?=

    Sub Dir, Virtual dir, what do I use?

    =?Utf-8?B?UnVkeQ==?=, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    431
    =?Utf-8?B?UnVkeQ==?=
    Jun 12, 2005
  2. Jean-Paul Calderone

    Re: getting object instead of string from dir()

    Jean-Paul Calderone, Dec 17, 2008, in forum: Python
    Replies:
    2
    Views:
    270
  3. Matthew Denner
    Replies:
    1
    Views:
    186
  4. Kga Agk
    Replies:
    2
    Views:
    153
    Kga Agk
    Jun 29, 2009
  5. Nick Gnedin
    Replies:
    2
    Views:
    167
Loading...

Share This Page