By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <c9**************************@posting.google.com >,
Noah <no**@noah.org> 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?

Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

P: n/a
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
Jul 18 '05 #4

P: n/a
> 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
Jul 18 '05 #5

P: n/a
Josiah Carlson <jc******@nospam.uci.edu> 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
Jul 18 '05 #6

P: n/a
Michael Hudson wrote:

Josiah Carlson <jc******@nospam.uci.edu> 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
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.