I have seen this similar question come up a few times via stackoverflow. Its is basically someone wanting to make "dynamically named variables". And it usually results in the same answer: There are usually better ways to solve the problem, and a dict is commonly one of them. You pretty much never want to actually generate dynamic variable that you have no reference to other than through globals() A dict is a good way to build key/value pairs on the fly. On Mar 12, 2014 9:15 PM, "Marcus Ottosson" <[email protected]> wrote:
> I honestly have no clue what it is you guys are trying to do. :O > > Is it possible to provide an alternate explanation? > > > On 12 March 2014 07:44, Joe Weidenbach <[email protected]> wrote: > >> It also helps if you declare the dictionary :) (That's what I get for >> typing on the fly when I'm about to crash for the night)--here's the >> corrected code: >> >> >> import maya.cmds as cmds >> >> import string >> >> fingers = ["index", "middle", "ring", "pinky", "thumb"] >> >> allTheLetters = string.uppercase >> >> namedFingers = {} >> >> >> for finger in fingers: >> >> for letter in range(4): >> >> namedFingers["{0}{1}".format(finger, allTheLetters[letter])] = >> "{0}{1}_".format(finger, allTheLetters[letter]) >> >> On 3/12/2014 12:42 AM, Joe Weidenbach wrote: >> >>> I've done exactly this! I don't know about variables directly, but this >>> does seem like a ready-made task for a dictionary. >>> >>> Try this (I haven't tested it, but can dig in my code if it doesn't >>> work): >>> >>> import maya.cmds as cmds >>> >>> import string >>> >>> fingers = ["index", "middle", "ring", "pinky", "thumb"] >>> >>> allTheLetters = string.uppercase >>> >>> for finger in fingers: >>> >>> for letter in range(4): >>> >>> namedFingers["{0}{1}".format(finger, allTheLetters[letter])] = >>> "{0}{1}_".format(finger, allTheLetters[letter]) >>> >>> >>> I think your version of string handling should work too, I'm just very >>> rusty on that method. >>> >>> Once you have this dictionary, you can just iterate through the keys to >>> get what you're looking for :) >>> >>> Joe >>> >>> On 3/12/2014 12:09 AM, [email protected] wrote: >>> >>>> I am trying to make variables with variables in their name. Is there >>>> any way to this? Or a better way. Right now I'm getting "# Error: can't >>>> assign to operator # SyntaxError: can't assign to operator #." >>>> >>>> This is a simple case but it will get much longer and I'd rather not >>>> have to have a big list of variables. >>>> >>>> """ >>>> >>>> import maya.cmds as cmds >>>> import string >>>> >>>> fingers = ["index", "middle", "ring", "pinky", "thumb"] >>>> allTheLetters = string.uppercase >>>> >>>> for finger in fingers: >>>> for letter in range(4): >>>> "%s%s" % (finger, allTheLetters[letter]) = "%s%s_" % (finger, >>>> allTheLetters[letter]) >>>> >>>> """ >>>> >>>> >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Python Programming for Autodesk Maya" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit https://groups.google.com/d/ >> msgid/python_inside_maya/5320105A.4020501%40gmail.com. >> >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > *Marcus Ottosson* > [email protected] > > -- > You received this message because you are subscribed to the Google Groups > "Python Programming for Autodesk Maya" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODYr5bsuNuRBe3qOhmJAfSmqfisS1xd4LiAy%3DUZWpYbcA%40mail.gmail.com<https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODYr5bsuNuRBe3qOhmJAfSmqfisS1xd4LiAy%3DUZWpYbcA%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0HU_oXkDqs%2BYKU9H%2Ba%3DPgE-xkbU-1cQnEN0OK8EH70GQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
