Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Variable definition

Reply
Thread Tools

Re: Variable definition

 
 
John Posner
Guest
Posts: n/a
 
      02-27-2010
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-27-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
 
Reply With Quote
 
 
 
 
John Posner
Guest
Posts: n/a
 
      02-27-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Automagic determination of definition based on definition location. Jon Slaughter C++ 4 10-26-2005 05:00 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 PM



Advertisments