Difference between type and class
Maric Michaud
maric at aristote.info
Thu Jul 31 09:18:03 EDT 2008
Le Thursday 31 July 2008 13:32:39 Thomas Troeger, vous avez écrit :
> De :
> Thomas Troeger <thomas.troeger.ext at siemens.com> (Aioe.org NNTP Server)
> À :
> python-list at python.org
> Date :
> Aujourd'hui 13:32:39
>
>
> > Can someone explain to me the difference between a type and a class?
>
> If your confusion is of a more general nature I suggest reading the
> introduction of `Design Patterns' (ISBN-10: 0201633612), under
> `Specifying Object Interfaces'.
>
> In short: A type denotes a certain interface, i.e. a set of signatures,
> whereas a class tells us how an object is implemented (like a
> blueprint). A class can have many types if it implements all their
> interfaces, and different classes can have the same type if they share a
> common interface. The following example should clarify matters:
>
Of course, this is what a type means in certain literature about OO
(java-ish), but this absolutely not what type means in Python. Types are a
family of object with a certain status, and they're type is "type",
conventionnaly named a metatype in standard OO.
There are three sort of objects in Python, in an inclusive order :
- ordinary object, or instance, they could not be instantiated or subclassed
(in general), and are all an instance of type "object" (or a subclass of it).
- types, or classes, are all instance of type 'type' (or a subclass of it),
they can be instantiated and they produce objects (ordinary object in
general) with theirslef as a type.
- metatypes or metaclass, are subclasses of "type", their instances are new
types.
For all tjis work together you must admit the following recursivity :
'type' is both a subclass and an instance of 'object' while 'object' is an
instance of 'type'.
--
_____________
Maric Michaud
More information about the Python-list
mailing list