[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

Raphael Mayoraz

3/1/2010 6:07:00 PM

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!
>
> -John
Thanks for your answer.
However, your solution changes the key name in the dictionary.
That's not what I want I need to do. What I want is to define a new
variable which name is define as a string: 'myPrefx' + key. In the example
I give, I would get 3 variables:
myPrefixred = a
myPrefixgreen = b
myPrefixblue = c

Raphael
1 Answer

Rhodri James

3/1/2010 11:06:00 PM

0

On Mon, 01 Mar 2010 18:07:17 -0000, Raphael Mayoraz <maygeo@netplus.ch>
wrote:

> Thanks for your answer.
> However, your solution changes the key name in the dictionary.
> That's not what I want I need to do. What I want is to define a new
> variable which name is define as a string: 'myPrefx' + key. In the
> example
> I give, I would get 3 variables:
> myPrefixred = a
> myPrefixgreen = b
> myPrefixblue = c

I repeat my previous question: why? What problem do you think this will
solve that will not be more easily solved working with the language rather
than against it?

--
Rhodri James *-* Wildebeeste Herder to the Masses