creating a new copy of a python class each time it's called
Question: I need some guidance on how to set this up correctly for what I'm trying to
do. I have a class called Attribute Block which I'll then use to create 3 or 4
Attribute block Objects. As seen below...

    
    
    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 objects

    
    
    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"),
        ]
    )
    

What I then want to do is be able to add one of these AttributeBlocks to an
Object, making sure when it's added, its a new instance of the AttributeBlock
so its sub attribute objects are new instances.

This is the object I will add my attribute blocks to.

    
    
    class ToyBox():
        def __init__(self, name="", attributes=[]):
            self.name = name
            self.attributes = attributes[:]
    
    newToyBox = ToyBox()
    newToyBox.name = "Jimmy"
    

**pseudo code**

    
    
    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"),
            ]
        )
    ]
    


Answer: If you want to make sure the Attribute instance added to your ToyBox is a
copy, the easiest way is to use the [standard copy
module](https://docs.python.org/2/library/copy.html)

    
    
    import copy
    ...
    class ToyBox(object):
        ...
        def add_attribute(self, attribute):
            self.attributes.append(copy.deepcopy(attribute))
    

