Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41111

RE: metatype

From Peter Otten <__peter__@web.de>
Subject RE: metatype
Date 2013-03-12 07:50 +0100
Organization None
References <BAY404-EAS28100DF680E616854A6CE44A3E10@phx.gbl> <BAY176-W39386733D837D0A95A985FA3E20@phx.gbl>
Newsgroups comp.lang.python
Message-ID <mailman.3219.1363070989.2939.python-list@python.org> (permalink)

Show all headers | View raw


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'>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

RE: metatype Peter Otten <__peter__@web.de> - 2013-03-12 07:50 +0100

csiph-web