Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 实例化变量中命名的对象_Python_Object_Instantiation - Fatal编程技术网

Python 实例化变量中命名的对象

Python 实例化变量中命名的对象,python,object,instantiation,Python,Object,Instantiation,我有一个组件对象库。我想在另一个对象中包含这些对象选择的实例化。但我希望将该选择作为列表提供,以便每次使用列表实例化容器对象时,都将使用其中指定的子对象创建它 假设我的组件库如下所示: class ColorBlob(object): ... def wipeItUp() ... class RedBlob(ColorBlob): ... def paintIt() ... class YellowBlob(ColorBlob):

我有一个组件对象库。我想在另一个对象中包含这些对象选择的实例化。但我希望将该选择作为列表提供,以便每次使用列表实例化容器对象时,都将使用其中指定的子对象创建它

假设我的组件库如下所示:

class ColorBlob(object):
    ...
    def wipeItUp()
        ...

class RedBlob(ColorBlob):
    ...
    def paintIt()
        ...
class YellowBlob(ColorBlob):
    ...
    def paintIt()
        ...
class BlueBlob(ColorBlob):
    ...
    def paintIt()
        ...
class Pallet(object):
    def __init__(self, colorList):
        for color in colorList:
            #Ok, here is where I get lost if I know the color I can do this:
            Pallet.BlueBlob = blobLib.BlueBlob()
            #But I don't, so I am trying to do something like this:
            blobSpecs       = getattr(blobLib, color)
            blobSpecs.Obj   = blobSpecs().returnObj(self.page) # with "returnObj" defined in the library as some other method
            setattr(self, Pallet.blobName, blobSpecs) #and I am completely lost.
我的容器对象如下所示:

class ColorBlob(object):
    ...
    def wipeItUp()
        ...

class RedBlob(ColorBlob):
    ...
    def paintIt()
        ...
class YellowBlob(ColorBlob):
    ...
    def paintIt()
        ...
class BlueBlob(ColorBlob):
    ...
    def paintIt()
        ...
class Pallet(object):
    def __init__(self, colorList):
        for color in colorList:
            #Ok, here is where I get lost if I know the color I can do this:
            Pallet.BlueBlob = blobLib.BlueBlob()
            #But I don't, so I am trying to do something like this:
            blobSpecs       = getattr(blobLib, color)
            blobSpecs.Obj   = blobSpecs().returnObj(self.page) # with "returnObj" defined in the library as some other method
            setattr(self, Pallet.blobName, blobSpecs) #and I am completely lost.
但我真正想在函数代码中做的是:

workingPallet=Pallet(['RedBlob', 'BlueBlob'])
workingPallet.RedBlob.paintIt()

当我尝试实例化容器中的子对象时,我知道我迷路了。有人能帮我纠正一下我的“getattr”和“setattr”胡说八道吗?

你几乎就到了,但问题不是你的
getattr
setattr
。您最终将类设置回
self
,而不是您创建的实例:

def __init__(self, colorList):
    for color in colorList:
        blobSpec       = getattr(blobLib, color)
        blob           = blobSpec()    # create an instance of the blob
        blob.Obj       = blob.returnObj(self.page) 
        setattr(self, color, blob)

这与直接调用类(
BlueBlob()
)是一样的,但是现在通过一个变量。

谢谢,我解决了caps问题,所以设置
setattr(self,color,blobSpecs)
对您不起作用?不,最后我给出了未绑定的方法。它有助于告诉我们你的想法去了哪里,哪里出了问题。在这里,一本字典可能会很有意义<代码>{cls.\uuuu name\uuuuu:ColorBlob中cls的cls.\uuuu子类\uuuuuu()}就是这样。创建blob的实例是我缺少的一点。应该是显而易见的。谢谢哦,我编辑了代码的最后一行,以包含调用该方法的“()”。为了让下一个来的人明白我的问题。