title: Defining a class object with type() : Why can't I use the name of an attribute meant to be deepcopied?
body: <p><strong>For some reasons I had to create a form class in Django by using the <code>type()</code> function.</strong></p>
  
  <p>The return statement of my function generator (<code>makeFurnitureForm()</code>) looked like this :</p>
  
  <pre><code>    return (type("FurnitureForm",
                   (forms.BaseForm, form_utils.forms.BetterBaseForm,),
                   {"_fieldsets" : dic, "base_fields" : fields,
                    "_fieldset_collection" : None, '_row_attrs' : {}}))
  </code></pre>
  
  <p>The class object I create inherits from two other classes.
  Here is the constructor of one of them : <a href="https://github.com/carljm/django-form-utils/blob/master/form_utils/forms.py#L191" rel="nofollow">form_utils.forms.BetterBaseForm</a></p>
  
  <pre><code>    def __init__(self, *args, **kwargs):
          self._fieldsets = deepcopy(self.base_fieldsets)
          self._row_attrs = deepcopy(self.base_row_attrs)
          self._fieldset_collection = None
          super(BetterBaseForm, self).__init__(*args, **kwargs)
  </code></pre>
  
  <p>Everything works fine with my code, but on a previous version, I used a code like this :</p>
  
  <pre><code>    return (type("FurnitureForm",
                   (forms.BaseForm, form_utils.forms.BetterBaseForm,),
                   {"base_fieldsets" : dic, "base_fields" : fields}))
  </code></pre>
  
  <p>Since <code>_fieldsets</code> and <code>_fieldset_collection</code> are explicitly defined and set in the <code>BetterBaseForm</code> constructor by deepcopying <code>base_fieldsets</code> attribute and assigning <code>None</code> value, I didn't understand why I got <code>AttributeError</code>s when trying to access <code>_fieldsets</code> and <code>_fieldset_collection</code> attribute of an instance (when rendering my django page for instance).</p>
  
  <h3>Here are my questions :</h3>
  
  <ol>
  <li><p>Doesn't <code>type()</code> calls base classes` constructors, and don't these constructors define and set attributes properly?</p></li>
  <li><p>Why did I have to explicitly set these attributes by myself?</p></li>
  <li><p>Why didn't I got an <code>AttributeError</code> when the constructor tries to
  access an attribute (<code>row_attrs</code> for instance) during the <code>type()</code>
  call? (I got one when trying to access it later though).</p></li>
  </ol>
  
  <p>I use my <code>makeFurnitureForm()</code> like this in my django view :</p>
  
  <pre><code>return (render(request,'main/devis-bis.html', {"form" : (makeFurnitureForm())()}))
  </code></pre>
  
  <p>The class object is directly passed to a view, and then to a template to be rendered.</p>
  
  <h1>EDIT : Traceback and code.</h1>
  
  <p>Example with this return :</p>
  
  <pre><code>    return (type("FurnitureForm",
                   (forms.BaseForm, form_utils.forms.BetterBaseForm,),
                   {"base_fieldsets" : dic, "base_fields" : fields,
                    "_fieldset_collection" : None, '_row_attrs' : {}}))
  </code></pre>
  
  <p>I also found out I directly pass a class object to the render() function, I don't understand why it even works because it expects an instance. I corrected it in the <code>render()</code> call.</p>
  
  <pre><code>File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/forms/forms.py" in __getitem__
    141.             field = self.fields[name]
  
  During handling of the above exception ('fieldsets'), another exception occurred:
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in _resolve_lookup
    883.                     current = current[bit]
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/forms/forms.py" in __getitem__
    144.                 "Key %r not found in '%s'" % (name, self.__class__.__name__))
  
  During handling of the above exception ("Key 'fieldsets' not found in 'FurnitureForm'"), another exception occurred:
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
    149.                     response = self.process_exception_by_middleware(e, request)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
    147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
  
  File "/nfs/2013/v/vmonteco/Code/web/demenhouse/htdocs/main/views.py" in secondform
    69.         return (render(request,'main/devis-bis.html', {"form" : makeFurnitureForm()}))
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/shortcuts.py" in render
    67.             template_name, context, request=request, using=using)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/loader.py" in render_to_string
    97.         return template.render(context, request)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/backends/django.py" in render
    95.             return self.template.render(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in render
    206.                     return self._render(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in _render
    197.         return self.nodelist.render(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in render
    992.                 bit = node.render_annotated(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in render_annotated
    959.             return self.render(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/loader_tags.py" in render
    173.         return compiled_parent._render(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in _render
    197.         return self.nodelist.render(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in render
    992.                 bit = node.render_annotated(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in render_annotated
    959.             return self.render(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/loader_tags.py" in render
    69.                 result = block.nodelist.render(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in render
    992.                 bit = node.render_annotated(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in render_annotated
    959.             return self.render(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/defaulttags.py" in render
    161.                 values = self.sequence.resolve(context, True)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in resolve
    709.                 obj = self.var.resolve(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in resolve
    850.             value = self._resolve_lookup(context)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/django/template/base.py" in _resolve_lookup
    891.                         current = getattr(current, bit)
  
  File "/nfs/2013/v/vmonteco/Envs/demenhouse/lib/python3.5/site-packages/form_utils/forms.py" in fieldsets
    253.                 self, self._fieldsets)
  
  Exception Type: AttributeError at /devisbis/1679091c5a880faf6fb5e6087eb1b2dc/
  Exception Value: 'FurnitureForm' object has no attribute '_fieldsets'
  </code></pre>