« Python - function v method | Main | Brand new hotel and training centre, Melksham »

October 21, 2006

Python - listing out the contents of all variables

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 incoemtion, then you could simply use the keys method ....

Posted by gje at October 21, 2006 07:40 AM

Comments

Post a comment

! Comment registration is required but no TypeKey token has been given in weblog configuration!
Well House Consultants Ltd. Copyright 2008