Too bad we can't edit our code on this forum...
Here's a more concise code to show what I want to do:
I have some code which uses vars() to assign a variable name/value
inside of a loop.
I works correctly in the script editor, but fails from within a Class.
In the SE:
------- CODE ------------------
prefix = 'preZZ2_'
myColors = ['green','indigo','violet']
violet = ''
for string in myColors:
# For each item, create a var and assign a value
vars()[string] = '%s_%s' % (prefix, string)
print string
green
# Result: 'preZZ2__green' #
violet
# Result: 'preZZ2__violet' #
------------ END ---------------
However, when I put the code in a class, the variable assignment is
lost once I'm outside the loop:
------------ CODE ---------------
prefix = 'pre_'
myColors = ['red','yellow','blue']
red = ''
class makeVars(object):
global red
red = ''
def create (self, prefix,myVars):
for string in myVars:
# For each item, create a var and assign a value
vars()[string] = '%s_%s' % (prefix, string)
print string
print 'red:',red
makeVars().create(prefix,myColors)
#green
#indigo
#violet
#red:
------------ END ---------------
Anyone know the fix (ie how to keep the assignment to 'red' in the
Class)??
Thanks
--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings:
http://groups.google.com/group/python_inside_maya/subscribe