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

N

Noah

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
 
C

Cameron Laird

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()
 
J

Josiah Carlson

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
 
P

Peter Hansen

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


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
 
J

Josiah Carlson

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
 
M

Michael Hudson

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
 
P

Peter Hansen

Michael said:
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.


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top