This question is based on Jabur's answer in this thread: What is a metaclass in Python?
You can achieve what you are looking for with the help of metaclasses.
First you need to create a metaclass:
def test_metaclass(name, bases, dict): print 'The Class Name is', name print 'The Class Bases are', bases print 'The dict has', len(dict), 'elems, the keys are', dict.keys() return dict
Of course, prints are not required.
Then let me introduce your new DunderDictHider:
class DunderDictHider(object): __metaclass__ = test_metaclass __dict__ = {'fake': 'dict'}
Now you have access to all initialized elements: repr(DunderDictHider)
Output (with line print repr(DunderDictHider)
):
The Class Name is DunderDictHider The Class Bases are (<type 'object'>,) The dict has 3 elems, the keys are ['__dict__', '__module__', '__metaclass__'] {'__dict__': {'fake': 'dict'}, '__module__': '__main__', '__metaclass__': <function test_metaclass at 0x1001df758>}
Every time you can try
if '__dict__' in repr(DunderDictHider)
to find out if this class is trying to hide its __dict__
or not. Remember that the resulting output is a string. This can be done better, but the idea itself.