What is the difference between 'type' and 'class'?

Stephen Hansen me+list/python at ixokai.io
Mon Jun 21 18:43:01 EDT 2010


On 6/21/10 3:11 PM, Peng Yu wrote:
> Are type and class synonyms? It seems that they are at least according
> to some webpages that I read. But I'm not completely sure. Could you
> let me know in case my impress is wrong?

Once upon a time, a type was something that was only built-in, provided
by Python, whereas a class was a user-created kind of object that other
objects could inherit from. You couldn't inherit from a type -- only a
class. That's why there were UserDict implementations, and such.

But that's not the case anymore. IIUC, a new-style class is for all
intents and purposes a user-defined type, and the two serve the same
function and are essentially the same.

An old-style class (a class which does not ultimately inherit from
'object') is a bit of a different beast with some different semantics
and exists just for backwards compatibility, I think. The unification of
classes and types occurred in Python 2.2 with PEP252 and PEP253.

There may be some very narrow little cracks where something is slightly
different between types and new-style classes, but I've never run into
it -- except that many types are fundamentally immutable(i.e., ints,
strings), and its awful hard to make an immutable class.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100621/09c8cde8/attachment-0001.sig>


More information about the Python-list mailing list