{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.
- >>> type(1)
<class 'int'>
>>> li = [] - >>> type(li)
<class 'list'>
>>> import odbchelper - >>> type(odbchelper)
<class 'module'> - >>> import types
>>> type(odbchelper) == types.ModuleType
True
>>>
- type takes anything −− and I mean anything −− and returns its datatype. Integers, strings, lists,dictionaries, tuples, functions, classes, modules, even types are acceptable.
- type can take a variable and return its datatype.
- type also works on modules.
- 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