Home

typeobject

Typeobject refers to the runtime representation of a type or class in Python. In Python's object model, every class or type is an instance of a metaclass, and the default metaclass is type. The built-in type object named type is at the root of the type hierarchy and governs core behavior of types and their instances.

Each type object describes a type's name, bases, method resolution order, and the set of attributes and

Type objects are used at runtime to query and manipulate types. The type() function has two roles:

Significance: Type objects are central to Python's dynamic nature, enabling introspection, dynamic class creation, and metaprogramming.

behaviors
it
implements.
In
CPython
the
type
object
is
implemented
as
the
C
struct
PyTypeObject
with
numerous
slots
that
describe
how
the
type
is
allocated,
deallocated,
and
how
its
instances
are
created,
initialized,
compared,
and
interacted
with.
The
type's
behavior
is
expressed
through
these
slots,
such
as
tp_new,
tp_init,
tp_call,
and
tp_getattro.
it
returns
the
type
of
an
object,
and
it
can
create
new
types
when
given
a
name,
bases,
and
dict.
For
example,
type('MyClass',
(Base,),
{'attr':
1})
creates
a
new
class.
Classes
expose
metadata
like
__name__,
__bases__,
and
__dict__,
and
participate
in
the
MRO.
The
metaclass
system
centers
on
type:
by
default,
classes
are
instances
of
type,
and
a
metaclass
can
customize
class
creation
by
deriving
from
type
or
by
declaring
a
different
metaclass.
They
underpin
core
mechanisms
such
as
class
construction,
the
descriptor
protocol,
and
the
overall
type
hierarchy.