Main Content
Python - listing out the contents of all variables Archive - Originally posted on "The Horse's Mouth" - 2006-10-21 07:40:02 - Graham Ellis
You can often guess which course I'm giving by the topic of the my daily writing ....
"How do I list out the contents of all variables with names starting v-a-r ... a question from yesterday.
The immediate answer:
# print all variables starting with "var"
sum = 9
var3 = 78
var6 = 99
varsity = 56
variable = "hello"
for name in dir():
if name.startswith("var"):
exec("print "+name)
A more thoughtful answer would be to suggest that you might have used a dictionary to store all this information, then you could simply use the keys method ....
Some other articles
Y115 - Additional Python Facilities Some gems from Intermediate Python Command line parameter handling in Python via the argparse module Json load from URL, recursive display, Python 3.4 Running an operating system command from your Python program - the new way with the subprocess module Json is the new marshall, pickle and cPickle / Python Python - an interesting application Handling JSON in Python (and a csv, marshall and pickle comparison) JSON from Python - first principles, easy example Teaching dilemma - old tricks and techniques, or recent enhancements? A demonstration of how many Python facilities work together Python regular expressions - repeating, splitting, lookahead and lookbehind Joining a MySQL table from within a Python program Factory methods and SqLite in use in a Python teaching example Running operating system commands from your Python program Python decorators - your own, staticmethod and classmethod Model - View - Controller demo, Sqlite - Python 3 - Qt4 Connecting Python to sqlite and MySQL databases Regular Expressions in Python Python - what is going on around me? Python - how it saves on compile time Serialization - storing and reloading objects Testing code in Python - doctest, unittest and others Python Regular Expressions A series of tyre damages Ignore case in Regular Expression Regular expressions made easy - building from components Turning objects into something you can store - Pickling (Python) Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP Sending an email from Python This article Python 3000 - the next generation Keeping your regular expressions simple Python to MySQL Splitting the difference What and why for the epoch Examples - Gadfly, NI Number, and Tcl to C interface The elegance of Python