title: Python - updating class attribute (monkey patching?)?
body: <p>So there is a class called <code>Field</code> and it has class attribute <code>_slots</code>, this attribute is dictionary.</p>
  
  <p>So it looks like this (that class also use custom <code>__metaclass__</code>):</p>
  
  <pre><code>class Field(object):
  
    _slots = {
      'key1': False,
      'key2': None,
      ...
  }
  </code></pre>
  
  <p>Class content is too big to be pasted here, so I'm providing a link to it, <a href="https://github.com/odoo/odoo/blob/8.0/openerp/fields.py#L290" rel="nofollow">src</a></p>
  
  <p>Now I need that when you instantiate this class, it will have my custom key/value pair inside that attribute.</p>
  
  <p>So I tried this (in my own module):</p>
  
  <pre><code>from openerp import fields
  
  fields.Field._slots['custom_key'] = None
  </code></pre>
  
  <p>When I print after this assignment, then it shows me that I have set this key/value pair, but actually when that class is loaded, it never gets my <code>custom_key</code>. Though if I monkey patch some method, it works, but for class attribute it does not. Is it even possible to do that (or am I doing something wrong here?)?</p>
  
  <p>Though if I write simple test scenario with my own simple class, like:</p>
  
  <p><strong>(f.py file)</strong></p>
  
  <pre><code>class F(object):
      _t = {'a': 1}
  </code></pre>
  
  <p><strong>(other python file)</strong></p>
  
  <pre><code>import f
  
  f.F._t['b'] = 10
  
  f_new = f.F()
  print f_new._t
  </code></pre>
  
  <p>Then this one works.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I also tried this (modifying code directly) in <code>MetaField</code> class that is used as <code>__metaclass</code> for <code>Field</code> (still that is ignored when <code>Field</code> class uses <code>_slots</code>:</p>
  
  <pre><code>def __new__(meta, name, bases, attrs):
      """ Combine the ``_slots`` dict from parent classes, and determine
      ``__slots__`` for them on the new class.
      """
      base_slots = {}
      for base in reversed(bases):
          base_slots.update(getattr(base, '_slots', ()))
  
      slots = dict(base_slots)
      slots.update(attrs.get('_slots', ()))
  
      attrs['__slots__'] = set(slots) - set(base_slots)
      attrs['_slots'] = slots
      attrs['_slots']['custom_key'] = None  # &lt;= Modification here
      return type.__new__(meta, name, bases, attrs)
  </code></pre>