Skip to content Skip to sidebar Skip to footer

Creating A Variable Name Dynamically

I have this code to create an interface and some buttons (python in maya) class mrShadowMapChangerUI: def __init__(self): smAttrs = ['shadowMap','smapResolution','smap

Solution 1:

It's just a syntax problem. Attributes specified in syntax must be identifiers, if you want generated attributes you'll need to use getattr or setattr (or delattr):

for attr, nice inzip(attrs, niceAttrs):
    setattr(self, attr, value)

Replace value with the value you want. This really has nothing to do with self: self is just another function argument and behaves like any other variable.

Solution 2:

How about setattr?

classFoo:
    def__init__(self):
        # Set attribute "bar" on this object to the number 1setattr(self, "bar", 1)

Post a Comment for "Creating A Variable Name Dynamically"