[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Variable definition

John Posner

2/27/2010 1:15:00 AM

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!

-John
2 Answers

Steven D'Aprano

2/27/2010 3:20:00 AM

0

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

John Posner

2/27/2010 3:34:00 AM

0

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