[Python-Dev] Adding types.build_class for 3.3

Nick Coghlan ncoghlan at gmail.com
Wed May 9 03:10:56 CEST 2012


On Wed, May 9, 2012 at 8:37 AM, Tres Seaver <tseaver at palladion.com> wrote:
>> No, the "mcl" in the call is just the designated metaclass - the
>> *actual* metaclass of the resulting class definition may be something
>> different. That's why this is a separate method from mcl.__new__.
>
> Why not make it a static method, if there is no notion of a useful 'cls'
> argument?

We need the explicitly declared metaclass as well as the bases in
order to determine the correct metaclass.

As a static method, the invocation would look like:

    type.build_class(mcl, bases, keywords, exec_body)

Since mcl will always be an instance of type in 3.x (due to all
classes being subtypes of object), it makes more sense to just make it
a class method and invoke the method on the declared metaclass:

    mcl.build_class(bases, keywords, exec_body)

The following assertion *does not* hold reliably:

    cls = mcl.build_class(bases, keywords, exec_body)
    assert type(cls) == mcl # Not guaranteed

Instead, the invariant that holds is the weaker assertion:

    cls = mcl.build_class(bases, keywords, exec_body)
    assert isinstance(cls, mcl)

This is due to the fact that one of the bases may specify that the
actual metaclass is a subtype of mcl rather than mcl itself.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list