__dict__s and types and maybe metaclasses...
Alex Martelli
aleax at mac.com
Thu May 3 01:06:21 EDT 2007
Adam Atlas <adam at atlas.st> wrote:
> On May 2, 5:24 pm, Larry Bates <larry.ba... at websafe.com> wrote:
> > I think that most people accomplish this by:
> >
> > class blah:
> > __initial_values={'a': 123, 'b': 456}
> >
> > def __init__(self):
> > self.__dict__.update(self.__initialvalues)
>
> That's not really what I'm talking about... I'm talking about
> replacing the __dict__ with a SUBCLASS of dict (not just default
> values), and that's at the time of the class declaration (the creation
> of `blah` as a `type` instance), not at instance creation.
The metaclass only enters the picture AFTER the class body has run (and
built a __dict__). As I explained many times:
class ic(bases):
<body>
means:
-- run the <body> to create a dictionary D
-- identify the metaclass M
-- execute:
ic = M("ic", bases, D)
the body always creates a dictionary -- you can't override that with
metaclasses. Maybe some deep bytecode hacks might work, but I have some
doubts in the matter (not given it much thought, tho).
Alex
More information about the Python-list
mailing list