Variable definition

John Posner jjposner at optimum.net
Sat Feb 27 04:33:53 CET 2010


On 2/26/2010 10:20 PM, Steven D'Aprano wrote:
> On Fri, 26 Feb 2010 20:15:16 -0500, John Posner wrote:
>
>> On 2/26/2010 6:32 PM, Raphael Mayoraz wrote:
>>> Hello,
>>>
>>> I'd like to define variables with some specific name that has a common
>>> prefix.
>>> Something like this:
>>>
>>> varDic = {'red': 'a', 'green': 'b', 'blue': 'c'} for key, value in
>>> varDic.iteritems(): 'myPrefix' + key = value
>>>
>>>
>> No trick, just swap a new key-value pair for each existing pair:
>>
>>     for key, value in varDic.iteritems():
>>         varDic[myPrefix + key] = value
>>         del varDict[key]
>>
>> Just make sure that *myPrefix* isn't an empty string!
>
> How does that answer the original poster's question?
>
> Admittedly, your solution is the Right Way To Do It, but what the OP
> wants is to go from a dict {'spam': 42} to a named variable myPrefixspam
> = 42, which is a totally bogus thing to do, but incredibly common among
> n00bs and refugees from horrible languages that allow that sort of
> abomination.

Yup, I misinterpreted the OP's intent. I guess my mind just couldn't 
encompass the enormity of his intended transgression.

-John




More information about the Python-list mailing list