Not totally clear what you're trying to do.

If you want to add something to the global namespace, you can use globals()
instead of vars().  In your first example globals() is vars(), but in the
second they aren't the same.

Otherwise, if you want to add a class attribute you can use
setattr(makeVars, 'red', whatever)

On Fri, Aug 19, 2011 at 9:16 AM, PixelMuncher <[email protected]> wrote:

> 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
>

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to