Re: Variable definition

Discussion in 'Python' started by John Posner, Feb 27, 2010.

  1. John Posner

    John Posner Guest

    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
    John Posner, Feb 27, 2010
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Feb 27, 2010
    #2
    1. Advertising

  3. John Posner

    John Posner Guest

    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
    John Posner, Feb 27, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    585
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    415
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    445
    Jon Slaughter
    Oct 26, 2005
  4. mfglinux
    Replies:
    11
    Views:
    697
    Roberto Bonvallet
    Sep 12, 2007
  5. Pierre Yves
    Replies:
    2
    Views:
    481
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page