Variable definition

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Feb 26 22:20:18 EST 2010


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.



-- 
Steven



More information about the Python-list mailing list