Making a variable with a variable in the name.

45 views
Skip to first unread message

ko1o3...@gmail.com

unread,
Mar 12, 2014, 3:09:18 AM3/12/14
to python_in...@googlegroups.com
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])

"""

Joe Weidenbach

unread,
Mar 12, 2014, 3:42:14 AM3/12/14
to python_in...@googlegroups.com
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

Joe Weidenbach

unread,
Mar 12, 2014, 3:44:26 AM3/12/14
to python_in...@googlegroups.com
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])

Marcus Ottosson

unread,
Mar 12, 2014, 4:15:09 AM3/12/14
to python_in...@googlegroups.com
I honestly have no clue what it is you guys are trying to do. :O

Is it possible to provide an alternate explanation?


--
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 python_inside_maya+unsub...@googlegroups.com.
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
konstr...@gmail.com

Justin Israel

unread,
Mar 12, 2014, 6:07:01 AM3/12/14
to python_in...@googlegroups.com

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.

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODYr5bsuNuRBe3qOhmJAfSmqfisS1xd4LiAy%3DUZWpYbcA%40mail.gmail.com.

Marcus Ottosson

unread,
Mar 12, 2014, 6:58:12 AM3/12/14
to python_in...@googlegroups.com
Ah, I see. Another way to accomplish it then might be the use of a class and setattr() and getattr() which takes a string as input, you'd get a dictionary of all available instance-attributes then via myinstance.__dict__

>> myinstance = MyClass()
>> setattr('my_dynamic_attribute', 'my value')
>> getattr('my_dynamic_attribute')
# 'my_value'
>> print "All attributes: %s" % myinstance.__dict__

You could then massage outgoing values from your custom-made attributes via overloading __getattr__ or similar to fit your needs.

However a dict is probably your best bet. :)



For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Marcus Ottosson

unread,
Mar 12, 2014, 7:00:25 AM3/12/14
to python_in...@googlegroups.com
Sorry, that's psuedo-code. Probably works better like this

>> setattr(myinstance, 'my_dynamic_attribute', 'my value')
>> getattr(myinstance, 'my_dynamic_attribute')

You could then of course access it via dot-notation
>> myinstance.my_dynamic_attribute
# 'my value'
--
Marcus Ottosson
konstr...@gmail.com

ko1o3...@gmail.com

unread,
Mar 12, 2014, 2:45:04 PM3/12/14
to python_in...@googlegroups.com
Thanks a lot guys, dict will work perfect. Saved me a lot of time.
Reply all
Reply to author
Forward
0 new messages