__dict__s and types and maybe metaclasses...

Steven Bethard steven.bethard at gmail.com
Wed May 2 19:51:02 EDT 2007


Adam Atlas wrote:
> Suppose I want to create a type (i.e. a new-style class via the usual
> `class blah(...)` mechanism) but, during the process of creating the
> type, I want to replace its __dict__

If I understand you right, what you want is something like::

     class MyDict(object):
         def __getitem__(self, key):
             ...
         def __setitem__(self, key, value):
             ...

     ... magic incantation to use a MyDict instance for class Foo ...
     class Foo(object):
         a = 1           # calls MyDict.__setitem__('a', 1)
         def bar(self):  # calls MyDict.__setitem__('bar', <func>)
             ...
         b = a + 2       # calls MyDict.__getitem__('a') and then
                         # calls MyDict.__setitem__('b', 3)

If that's right, then the answer is "no, you can't do this". There was 
some discussion of allowing such a thing in Python 3.0, but it fizzled. 
(Check the python-3000 archives if you're interested.)


So what's the real problem you're trying to solve?

STeVe



More information about the Python-list mailing list