【发布时间】:2015-11-24 14:15:40
【问题描述】:
我需要一些指导,了解如何正确设置它以完成我正在尝试做的事情。 我有一个名为属性块的类,然后我将使用它来创建 3 或 4 个属性块对象。如下图...
class AttributeBlock():
def __init__(self, key, label, isClosed, isRequired, attributes):
self.key = key
self.label = label
self.isClosed = isClosed
self.isRequired = isRequired
self.attributes = attributes if attributes is not None else {}
3 属性块对象
AttributeBlock(
key="Sphere",
isRequired=True,
attributes=[
''' Other class objects '''
BoolProperty("ishidden", False, "Hidden"),
]
)
AttributeBlock(
key="Box",
isRequired=True,
attributes=[
''' Other class objects '''
BoolProperty("ishidden", True, "Hidden"),
]
)
AttributeBlock(
key="Circle",
isRequired=False,
attributes=[
''' Other class objects '''
BoolProperty("ishidden", True, "Hidden"),
]
)
然后我想要做的是能够将这些 AttributeBlocks 之一添加到一个对象中,确保在添加它时,它是 AttributeBlock 的一个新实例,因此它的子属性对象是新实例。
这是我将添加属性块的对象。
class ToyBox():
def __init__(self, name="", attributes=[]):
self.name = name
self.attributes = attributes[:]
newToyBox = ToyBox()
newToyBox.name = "Jimmy"
伪代码
def add_attribute_block(toybox = None, key = "" ):
if an AttributeBlock with the matching key exists:
add it to toybox.attributes
add_attribute_block( newToyBox, "Box" )
print newToyBox
>>
ToyBox
name="Jimmy"
attributes=[
AttributeBlock(
key="Box",
isRequired=True,
attributes=[
BoolProperty("ishidden", True, "Hidden"),
]
),
AttributeBlock(
key="Sphere",
isRequired=True,
attributes=[
BoolProperty("ishidden", True, "Hidden"),
]
)
]
【问题讨论】: