implicit variable declaration and access
Tom Anderson
twic at urchin.earth.li
Tue Jun 14 06:42:27 EDT 2005
On Mon, 13 Jun 2005, Ali Razavi wrote:
> Tom Anderson wrote:
>> On Mon, 13 Jun 2005, Ali Razavi wrote:
>>
>>> Is there any reflective facility in python that I can use to define a
>>> variable with a name stored in another variable ?
>>>
>>> like I have :
>>> x = "myVarName"
>>>
>>> what can I do to declare a new variable with the name of the string stored
>>> in x. And how can I access that implicitly later ?
>>
>> Are you absolutely sure you want to do this?
>
> Have you ever heard of meta programming ? I guess if you had, it
> wouldn't seem this odd to you.
Oh, i have. It's just that i've also seen about a billion posts from
novice programmers asking exactly that question, when it turns out what
they really want is a dictionary, or sometimes a list. If that was the
case, answering your question would not be solving your problem, an i'd
rather solve your problem.
If it's not, try:
x = "myVarName"
y = "myVarValue"
locals()[x] = y
What did you mean by "And how can I access that implicitly later?"?
tom
--
I content myself with the Speculative part [...], I care not for the Practick. I seldom bring any thing to use, 'tis not my way. Knowledge is my ultimate end. -- Sir Nicholas Gimcrack
More information about the Python-list
mailing list