The fundamental types of Python – a diagram

April 3rd, 2012 at 8:33 pm

The aim of this post is to present a succinct diagram that correlates some basic properties of all Python objects with the fundamental types type and object. This is not a tutorial – it’s more of a reference snapshot that puts things in order. To properly understand why things are the way they are, check out the existing and future writings in the Python internals category of this blog, as well as other resources available online.

In Python, every object has a type. Types are also objects – rather special objects. A type object, like any other object, has a type of its own. It also has a sequence of "base types" – in other words, types from which it inherits. This is unlike non-type objects, which don’t have base types.

Consider this exemplary piece of code (Python 3):

# Some types
class Base:
    pass

class Klass(Base):
    pass

class Meta(type):
    pass

class KlassWithMeta(metaclass=Meta):
    pass

# Non-types
kwm = KlassWithMeta()
mylist = []

The following diagram describes the types and bases of all the objects created in this code. Non-type objects only have types and no bases:

http://eli.thegreenplace.net/wp-content/uploads/2012/04/typediagram.png

Some interesting things to note:

  • The default type of all new types is type. This can be overridden by explicitly specifying the metaclass for a type.
  • Built-in types like list and user-defined types like Base are equivalent as far as Python is concerned.
  • The special type type is the default type of all objects – including itself. It is an object, and as such, inherits from object.
  • The special type object is the pinnacle of every inheritance hierarchy – it’s the ultimate base type of all Python types.
  • type and object are the only types in Python that really stand out from other types (and hence they are colored differently). type is its own type. object has no base type.

Related posts:

  1. Python objects, types, classes, and instances – a glossary
  2. Python object creation sequence
  3. Under the hood of Python class definitions
  4. Python metaclasses by example
  5. Fundamental concepts of plugin infrastructures

5 Responses to “The fundamental types of Python – a diagram”

  1. Ram RachumNo Gravatar Says:

    Awesome diagram!

    I take pride in having once created a double-metaclass, i.e. a metaclass of a metaclass:

    https://github.com/cool-RR/GarlicSim/blob/development/garlicsim/garlicsim/general_misc/context_managers/context_manager_type_type.py

    We use this code in production in my work.

  2. jan Kaiiszewski (zuo)No Gravatar Says:

    Shouldn’t a blue arrow from ‘type’ to ‘object’ also be placed in the diagram? Please note that both ‘isinstance(type, object)’ and ‘isinstance(object, type)’ are true.

  3. elibenNo Gravatar Says:

    jan,

    isinstance also checks for inheritance, hence the result you see. For the diagram above, isinstance(Klass(), Base) would also be true.

  4. AnilNo Gravatar Says:

    Thanks for the easy to understand pictorial representation. It helped me a lot in understanding python default types.

    Keep up the good work :-)

  5. FilipeNo Gravatar Says:

    Hi. Here’s my take on it: http://blog.invisivel.net/2012/04/10/pythons-object-model-explained/
    It’s quite close to yours, but I’m using the notation for UML class diagrams.

Leave a Reply

To post code with preserved formatting, enclose it in `backticks` (even multiple lines)