Hi, I am wondering if there is a way to somehow look at the contents of a variable and make a variable name with the contents of that variable. I really don't know how else to word that, just wondering. If anyone understands what I am saying and knows an answer please post. Thanks, 26bm

Recommended Answers

It is possible for global variables

>>> varname = 'thevalue'
>>> globals()[varname] = 3.14
>>> thevalue
3.14

In principle it doesn't work for local variables. Dictionaries returned by locals() are read-only.

Jump to Post

All 3 Replies

It is possible for global variables

>>> varname = 'thevalue'
>>> globals()[varname] = 3.14
>>> thevalue
3.14

In principle it doesn't work for local variables. Dictionaries returned by locals() are read-only.

thanks

And 999 out of 1000 times you should not be using this but you have bad design so do not shy away on explaining reason for this trickery, so we may recommend bretter way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.