{article Dive into Python}{title} {text}{/article}

With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.__class__.

The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.

  1. >>> type(1)
    <class 'int'>
    >>> li = []
  2. >>> type(li)
    <class 'list'>
    >>> import odbchelper
  3. >>> type(odbchelper)
    <class 'module'>
  4. >>> import types
    >>> type(odbchelper) == types.ModuleType
    True
    >>>

  1. type takes anything −− and I mean anything −− and returns its datatype. Integers, strings, lists,dictionaries, tuples, functions, classes, modules, even types are acceptable.
  2. type can take a variable and return its datatype.
  3. type also works on modules.
  4. You can use the constants in the types module to compare types of objects. This is what the info
    function does, as you'll see shortly