So there is a class called Field
and it has class attribute _slots
, this attribute is dictionary.
So it looks like this (that class also use custom __metaclass__
):
class Field(object):
_slots = {
'key1': False,
'key2': None,
...
}
Class content is too big to be pasted here, so I'm providing a link to it, src
Now I need that when you instantiate this class, it will have my custom key/value pair inside that attribute.
So I tried this (in my own module):
from openerp import fields
fields.Field._slots['custom_key'] = None
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 custom_key
. 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?)?
Though if I write simple test scenario with my own simple class, like:
(f.py file)
class F(object):
_t = {'a': 1}
(other python file)
import f
f.F._t['b'] = 10
f_new = f.F()
print f_new._t
Then this one works.
Update
I also tried this (modifying code directly) in MetaField
class that is used as __metaclass
for Field
(still that is ignored when Field
class uses _slots
:
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 # <= Modification here
return type.__new__(meta, name, bases, attrs)