Print all variables and their values
globals()
returns a dict
- so, to iterate through a dict
's key/value pairs, you call it's items()
method:
dog = 'cat'
>>> for name, value in globals().items():
... print(name, value)
...
('__builtins__', <module '__builtin__' (built-in)>)
('__name__', '__main__')
('dog', 'cat')
('__doc__', None)
('__package__', None)
>>>
This is explained within the docs for Data Structures, available here!
Python 3 version:
for name, value in globals().copy().items():
print(name, value)
if we do not copy the output globals functions then there will be RuntimeError.
Also if there are list and dictionaries in the output of some variable then use deepcopy() instead of copy()
for name, value in globals().deepcopy().items():
print(name, value)
Getting All Variables In Scope
No. "In scope" variables are determined by the "scope chain", which is not accessible programmatically.
For detail (quite a lot of it), check out the ECMAScript (JavaScript) specification. Here's a link to the official page where you can download the canonical spec (a PDF), and here's one to the official, linkable HTML version.
Update based on your comment to Camsoft
The variables in scope for your event function are determined by where you define your event function, not how they call it. But, you may find useful information about what's available to your function via this
and arguments by doing something along the lines of what KennyTM pointed out (for (var propName in ____)
) since that will tell you what's available on various objects provided to you (this
and arguments; if you're not sure what arguments they give you, you can find out via the arguments
variable that's implicitly defined for every function).
So in addition to whatever's in-scope because of where you define your function, you can find out what else is available by other means by doing:
var n, arg, name;
alert("typeof this = " + typeof this);
for (name in this) {
alert("this[" + name + "]=" + this[name]);
}
for (n = 0; n < arguments.length; ++n) {
arg = arguments[n];
alert("typeof arguments[" + n + "] = " + typeof arg);
for (name in arg) {
alert("arguments[" + n + "][" + name + "]=" + arg[name]);
}
}
(You can expand on that to get more useful information.)
Instead of that, though, I'd probably use a debugger like Chrome's dev tools (even if you don't normally use Chrome for development) or Firebug (even if you don't normally use Firefox for development), or Dragonfly on Opera, or "F12 Developer Tools" on IE. And read through whatever JavaScript files they provide you. And beat them over the head for proper docs. :-)
View list of all JavaScript variables in Google Chrome Console
Is this the kind of output you're looking for?
for(var b in window) {
if(window.hasOwnProperty(b)) console.log(b);
}
This will list everything available on the window
object (all the functions and variables, e.g., $
and jQuery
on this page, etc.). Though, this is quite a list; not sure how helpful it is...
Otherwise just do window
and start going down its tree:
window
This will give you DOMWindow
, an expandable/explorable object.
How to get a list of variables in specific Python module?
print [item for item in dir(adfix) if not item.startswith("__")]
Is usually the recipe for doing this, but it begs the question.
Why?
List of Defined Variables in R
ls()
From the help page:
‘ls’ and ‘objects’ return a vector of character strings giving the
names of the objects in the specified environment. When invoked
with no argument at the top level prompt, ‘ls’ shows what data
sets and functions a user has defined. When invoked with no
argument inside a function, ‘ls’ returns the names of the
functions local variables. This is useful in conjunction with
‘browser’.
Edit: I should note that to list ALL variables you would need to use
ls(all.names = TRUE)
otherwise variables that begin with a dot won't show up in the listing.
Related Topics
Name This Python/Ruby Language Construct (Using Array Values to Satisfy Function Parameters)
Why Is Equivalent Python Code So Much Slower
Numpy Selecting Specific Column Index Per Row by Using a List of Indexes
How to Write to a CSV Line by Line
Type Hinting a Collection of a Specified Type
How to Make a Cross-Module Variable
Python and Regular Expression with Unicode
Weighted Random Selection with and Without Replacement
Priority of the Logical Operators Not, And, or in Python
Calling Java/Scala Function from a Task
Using Strides for an Efficient Moving Average Filter
Str' Object Does Not Support Item Assignment
Dictionaries and Default Values
List Comprehension with If Statement