473,383 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
6 5857
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
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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Max | last post by:
Yeah, I know. It's the price we pay for forsaking variable declarations. But for java programmers like me, Py's scoping is too complicated. Please explain what constitutes a block/namespace, and...
53
by: Stelios Xanthakis | last post by:
Hi. pyvm is a program which can run python 2.4 bytecode (the .pyc files). A demo pre-release is available at: http://students.ceid.upatras.gr/~sxanth/pyvm/ Facts about pyvm: - It's FAST....
0
by: Bryan | last post by:
hi, i'm trying to write a multithreaded embedded python application and i'm having some trouble. i found this article "embedding python in multi-threaded c/c++ applications" in the python...
0
by: ycollet | last post by:
Hello, I'm trying to write a program to send python statements to a python server via tcp and then get back results via a tcp connection. It nearly works ... but I'm totally lost with the...
4
by: carl.dhalluin | last post by:
Hello I am completely puzzled why the following exec code does not work: mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)" def execute(): exec mycode execute()
10
by: Lorenzo Di Gregorio | last post by:
Hello, I've been using Python for some DES simulations because we don't need full C speed and it's so much faster for writing models. During coding I find it handy to assign a variable *unless...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.