[Python-3000] Metaclasses in Python 3000: Draft 2

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Mar 15 23:48:50 CET 2007


Phillip J. Eby wrote:

> > Maybe the PEP should specify a protocol that the
> > pseudo-dict must provide for doing this, such as an
> > iteritems() method (or whatever the Py3k equivalent
> > is).
> 
> That's not necessary; the metaclass instance __call__ (or class __new__) 
> should be responsible for passing a real dictionary to type.__new__,

I was thinking that there could be a default mechanism
used by type.__new__ to generate a dict if it wasn't
passed a real dict. This would relieve some burden from
the majority of metaclasses, I think. If the custom
namespace is subclassed from dict or DictMixin or whatever
ABC there is for this in Py3k, then it will already support
the needed protocol.

To be clearer, I perhaps should say that the protocol is
only required if the metaclass wants to make use of this
default behaviour. If the metaclass is willing to do its
own dict extraction, then it doesn't matter.

Equivalently, this could be phrased in terms of the
protocol that must be supported by the object passed to
type.__new__, i.e. must either be exactly a dict or
be able to have a dict created from it by calling
dict() on it. Then it's up to the metaclass how it
satisfies this. And if the "metaclass" is really a
function, type.__new__ isn't involved at all, so none
of this applies.

--
Greg


More information about the Python-3000 mailing list