Difference between type and class

Miles semanticist at gmail.com
Fri Aug 1 00:58:24 CEST 2008

On Thu, Jul 31, 2008 at 1:59 PM, Nikolaus Rath wrote:
> If it is just a matter of different rendering, what's the reason for
> doing it like that? Wouldn't it be more consistent and straightforward
> to denote builtin types as classes as well?

Yes, and in Python 3, it will be so:

>>> class myint(int): pass
>>> int
<class 'int'>
>>> myint
<class '__main__.myint'>

The reason the distinction is made currently is that before Python
2.2, "types" were built-in (or C extension) classes, and "classes"
were Python classes; it was not possible to subclass built-in types.
That "classic" style of classes are still supported in Python 2.2 and
above (but not in Python 3), by not inheriting from object or any
other built-in.  However, for new-style classes, the only distinction
is in the repr.

>>> class classic: pass
>>> class newstyle(object): pass
>>> type(classic)
<type 'classobj'>
>>> type(classic())
<type 'instance'>
>>> classic.__class__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: class classic has no attribute '__class__'
>>> classic().__class__
<class __main__.classic at 0x64e70>
>>> type(newstyle)
<type 'type'>
>>> type(newstyle())
<class '__main__.newstyle'>

Further reading:



More information about the Python-list mailing list