metatype

Peter Otten __peter__ at web.de
Tue Mar 12 07:50:21 CET 2013


shangyu wrote:

> Hi dear all,
> I have following Python code
> class mydict(dict):
>     def __init__(self):
>         pass
> I wonder how this new type get created . What is the type of metatype in
> the following line ? type = (PyTypeObject *)metatype->tp_alloc(metatype,
> nslots); (line 2296 of typeobject.c Python2.7.3 source code)
> It seems PyDict_Type . If so , how do I expect the tp_alloc will return a
> PyTypeObject object ? Maybe I've missed something ? Many thanks!!!

> I think I've found it out . For new-style class it's PyType_Type and for
> old-style class it's PyClass_Type . Thanks anyway.

Yes. You can find that out without resorting to the C API:

>>> class A(dict): pass
... 
>>> type(A)
<type 'type'>

A fancy example:

>>> import abc
>>> class B:
...     __metaclass__ = abc.ABCMeta
... 
>>> type(B)
<class 'abc.ABCMeta'>





More information about the Python-list mailing list