English 中文(简体)
动态地创造可变的名称
原标题:creating a variable name dynamically

我有这部法典,以建立一个接口和一些纽扣州(在市长中排出)

class mrShadowMapChangerUI:
    def __init__(self):

        smAttrs = [ shadowMap , smapResolution , smapSamples , smapSoftness , smapBias ]
        smNiceAttrs = [ Active , Resolution , Samples , Softness , Bias ]
        attrs = zip(smAttrs,smNiceAttrs)

        self.form = mc.columnLayout()

        self.smapResolutionField =  mc.textFieldButtonGrp(   label=attrs[1][1],  text=int(defaultLightValues[1]),        bc=Callback(self.setSmValue, attrs[1][0]))
        self.smapSamplesField =     mc.textFieldButtonGrp(   label=attrs[2][1],  text=int(defaultLightValues[2]),        bc=Callback(self.setSmValue, attrs[2][0]))
        self.smapSoftnessField =    mc.textFieldButtonGrp(   label=attrs[3][1],  text=( %.3f  % defaultLightValues[3]),  bc=Callback(self.setSmValue, attrs[3][0]))
        self.smapBiasField =        mc.textFieldButtonGrp(   label=attrs[4][1],  text=( %.3f  % defaultLightValues[4]),  bc=Callback(self.setSmValue, attrs[4][0]))

并且我要谈谈这样一些事情,以自动地创建纽特州,知道他们的名字(因此,我可以稍后地向他们提问)。

class mrShadowMapChangerUI:
    def __init__(self):

        smAttrs = [ shadowMap , smapResolution , smapSamples , smapSoftness , smapBias ]
        smNiceAttrs = [ Active , Resolution , Samples , Softness , Bias ]
        attrs = zip(smAttrs,smNiceAttrs)

        self.form = mc.columnLayout()
        for attr in attrs:
            self.( %s  % attr[0]) =  mc.textFieldButtonGrp(   label=attr[1],  text=int(defaultLightValues[1]),        bc=Callback(self.setSmValue, attr[0]))

        mc.showWindow(self.window)

我确实在理解所有这些“自食其力”的工作方面遇到麻烦,因此,我大概没有基本的东西,但我迄今所尝试的所有东西都没有奏效。

感谢!

最佳回答

它只是一个yn子问题。 如果你想要产生需要使用<代码>getattr <>/code>或setattr(或delattr):

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

用你想要的价值取代<条码>。 这确实与<条码>本身无关:<条码>自封<条码>只是另一种功能论据,与任何其他变量一样。

问题回答

How about :code>setattr ?

class Foo:
    def __init__(self):
        # Set attribute "bar" on this object to the number 1
        setattr(self, "bar", 1)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签