我有一个组件对象库。我想在另一个对象中包含这些对象的选择实例。但我想将该选择作为列表提供,以便每次使用列表实例化容器对象时,都会在其中创建指定的子对象。
假设我的组件库如下所示:
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”废话吗?