I don't really understand how the base metaclass works (aka type). Does anyone know of a pure-python analogue for its functionality?
The python docs often do this for C-level code that is hard to fully describe in english (for example, see the explaination of __getattribute__), but not for type.
I do know how to get started. Since defining the behavior of type using a subclass of type would be a bit like saying "type works the way type works", I define a duck-typed metaclass. It works some, but not enough.
class MetaClassDuck(object):
    @classmethod
    def __new__(self, mcs, name, bases, attrs):
        """Create a new class object."""
        newcls = super(MetaClassDuck, self).__new__(mcs)
        newcls.__dict__.update(attrs)
        newcls.__name__ = name
        newcls.__bases__ = bases
        return newcls
    def __call__(cls, *args, **kwargs):
        """Calling a class results in an object instance."""
        ###########################################################
        # Fill in the blank:
        # I don't see a way to implement this without type.__new__
        ###########################################################
        return newobj
class MyClass(object):
    __metaclass__ = MetaClassDuck
    one = 1
    _two = 2
    @property
    def two(self):
        return self._two
# This bit works fine.
assert type(MyClass) is MetaClassDuck
assert MyClass.one == 1
assert isinstance(MyClass.two, property)
myobj = MyClass()
# I crash here:
assert myobj.one == 1
assert myobj.two == 2
class MyClass2(MyClass):
    three = 3
assert type(MyClass2) is MetaClassDuck
assert MyClass2.one == 1
assert isinstance(MyClass2.two, property)
assert MyClass2.three == 3
myobj2 = MyClass2()
assert myobj2.one == 1
assert myobj2.two == 2
assert myobj2.three == 3
 
     
    