Sunday, September 4, 2011

See declared variables in python

For those using python for some time, you might miss some tricks that are present in many other script languages such as Perl and Bash.

One that I miss very much in dynamical naming of variables. You can easily overcome this problem using dictionaries:
d = {}

d['foo'] = 'bar'

But python names its variables by storing them on a global dictionary named vars(). So you can create a variable by naming it after a string

vars()['foo'] = 'bar'

print foo

I don't know when one might need this, but it is doable.

No comments:

Post a Comment

 
Locations of visitors to this page