How do I access Python's dictionary of all global variables?

Discussion in 'Python' started by Noah, Feb 21, 2004.

  1. Noah

    Noah Guest

    I thought that Python has a builtin dictionary that associates
    global variable names with their values. I have forgotten how to do this and
    I can't seem to come up with the right search keywords to locate this secret
    again. I'm trying to write a global dynamic variable debugger sort of thing.

    So for example you could have code that looked something akin to this:
    >>> x = 5
    >>> y = "hello"
    >>> z = [1,2,3]
    >>> print __VARS__['x'], __VARS__['y'], __VARS__['z']

    x hello [1, 2, 3]

    Is it possible to iterate through all variables in all scopes in all objects?

    Yours,
    Noah
    Noah, Feb 21, 2004
    #1
    1. Advertising

  2. In article <>,
    Noah <> wrote:
    >I thought that Python has a builtin dictionary that associates
    >global variable names with their values. I have forgotten how to do this and
    >I can't seem to come up with the right search keywords to locate this secret
    >again. I'm trying to write a global dynamic variable debugger sort of thing.
    >
    >So for example you could have code that looked something akin to this:
    >>>> x = 5
    >>>> y = "hello"
    >>>> z = [1,2,3]
    >>>> print __VARS__['x'], __VARS__['y'], __VARS__['z']

    >x hello [1, 2, 3]
    >
    >Is it possible to iterate through all variables in all scopes in all objects?

    .
    .
    .
    Start with
    print globals()
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
    Cameron Laird, Feb 21, 2004
    #2
    1. Advertising

  3. Cameron already dealt with globals, so I'll try to tackle this one.

    > Is it possible to iterate through all variables in all scopes in all objects?


    Quick answer: no

    Long answer: you would need to get access to every object pointer in the
    entirety of the interpreter. I'm sure this could probably be done with
    a C extension module that returned a list of every object in existance
    by hooking into the garbage collector, but you really don't want to do
    this, because you'd get references to EVERYTHING, including functions,
    methods, C extension functions, etc.

    A better idea would be to use weakrefs to keep references to every live
    object that you care about, and if you desire, search through those:

    class myobject:
    __olist = {}
    def __init__(self):
    #create a weak reference to yourself
    #place it in the __olist
    self.__olist[id(self)] = ref(self)
    def __del__(self):
    del self.__olist[id(self)]
    def get_olist(self):
    #this creates a hard reference to every object
    return [o() for o in self.__olist.values() if o() is not None]


    - Josiah
    Josiah Carlson, Feb 21, 2004
    #3
  4. Noah

    Peter Hansen Guest

    Josiah Carlson wrote:
    >
    > Cameron already dealt with globals, so I'll try to tackle this one.
    >
    > > Is it possible to iterate through all variables in all scopes in all objects?

    >
    > Quick answer: no
    >
    > Long answer: you would need to get access to every object pointer in the
    > entirety of the interpreter.


    For reference, this can be done using a debug build of the interpreter.
    I don't recall the name of the function or even which module it was
    in (sys or gc, presumably) but it was there. (I think. :)

    -Peter
    Peter Hansen, Feb 23, 2004
    #4
  5. > For reference, this can be done using a debug build of the interpreter.
    > I don't recall the name of the function or even which module it was
    > in (sys or gc, presumably) but it was there. (I think. :)


    That scares me, it is very dangerous.

    - Josiah
    Josiah Carlson, Feb 24, 2004
    #5
  6. Josiah Carlson <> writes:

    > > For reference, this can be done using a debug build of the interpreter.
    > > I don't recall the name of the function or even which module it was
    > > in (sys or gc, presumably) but it was there. (I think. :)


    There's gc.get_objects(), present in all builds, which gives a list of
    all objects tracked by the cycle collector (i.e. all containers).

    In a debug build there's sys.getobjects(), which gives a list of
    (potentially, depending on arguments passed) all objects known to the
    interpreter.

    > That scares me, it is very dangerous.


    Why? It can be useful for tracking refleak problems, if nothing else.

    Cheers,
    mwh

    --
    I have a cat, so I know that when she digs her very sharp claws into
    my chest or stomach it's really a sign of affection, but I don't see
    any reason for programming languages to show affection with pain.
    -- Erik Naggum, comp.lang.lisp
    Michael Hudson, Feb 24, 2004
    #6
  7. Noah

    Peter Hansen Guest

    Michael Hudson wrote:
    >
    > Josiah Carlson <> writes:
    >
    > > > For reference, this can be done using a debug build of the interpreter.
    > > > I don't recall the name of the function or even which module it was
    > > > in (sys or gc, presumably) but it was there. (I think. :)

    >
    > There's gc.get_objects(), present in all builds, which gives a list of
    > all objects tracked by the cycle collector (i.e. all containers).
    >
    > In a debug build there's sys.getobjects(), which gives a list of
    > (potentially, depending on arguments passed) all objects known to the
    > interpreter.
    >
    > > That scares me, it is very dangerous.

    >
    > Why? It can be useful for tracking refleak problems, if nothing else.


    I think he thought I was suggesting that someone might actually want
    to use this in real code, rather than in debugging. Clearly my use of
    "for reference" didn't adequately suggest that I also thought that
    would be nuts.

    -Peter
    Peter Hansen, Feb 24, 2004
    #7
    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. Wayne
    Replies:
    2
    Views:
    456
    Wayne
    Nov 11, 2003
  2. Crutcher
    Replies:
    1
    Views:
    356
    Alex Martelli
    Dec 31, 2005
  3. jubelbrus
    Replies:
    5
    Views:
    600
    JohnQ
    Jul 20, 2007
  4. icarus
    Replies:
    3
    Views:
    519
    George Sakkis
    Sep 27, 2008
  5. Mr.SpOOn
    Replies:
    3
    Views:
    276
    Arnaud Delobelle
    Oct 26, 2008
Loading...

Share This Page