/Doc/library/types.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 258 lines · 138 code · 120 blank · 0 comment · 0 complexity · 6bee22828dfec5a0b52e65ab6a84815d MD5 · raw file

  1. :mod:`types` --- Names for built-in types
  2. =========================================
  3. .. module:: types
  4. :synopsis: Names for built-in types.
  5. This module defines names for some object types that are used by the standard
  6. Python interpreter, but not for the types defined by various extension modules.
  7. Also, it does not include some of the types that arise during processing such as
  8. the ``listiterator`` type. It is safe to use ``from types import *`` --- the
  9. module does not export any names besides the ones listed here. New names
  10. exported by future versions of this module will all end in ``Type``.
  11. Typical use is for functions that do different things depending on their
  12. argument types, like the following::
  13. from types import *
  14. def delete(mylist, item):
  15. if type(item) is IntType:
  16. del mylist[item]
  17. else:
  18. mylist.remove(item)
  19. Starting in Python 2.2, built-in factory functions such as :func:`int` and
  20. :func:`str` are also names for the corresponding types. This is now the
  21. preferred way to access the type instead of using the :mod:`types` module.
  22. Accordingly, the example above should be written as follows::
  23. def delete(mylist, item):
  24. if isinstance(item, int):
  25. del mylist[item]
  26. else:
  27. mylist.remove(item)
  28. The module defines the following names:
  29. .. data:: NoneType
  30. The type of ``None``.
  31. .. data:: TypeType
  32. .. index:: builtin: type
  33. The type of type objects (such as returned by :func:`type`); alias of the
  34. built-in :class:`type`.
  35. .. data:: BooleanType
  36. The type of the :class:`bool` values ``True`` and ``False``; alias of the
  37. built-in :class:`bool`.
  38. .. versionadded:: 2.3
  39. .. data:: IntType
  40. The type of integers (e.g. ``1``); alias of the built-in :class:`int`.
  41. .. data:: LongType
  42. The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`.
  43. .. data:: FloatType
  44. The type of floating point numbers (e.g. ``1.0``); alias of the built-in
  45. :class:`float`.
  46. .. data:: ComplexType
  47. The type of complex numbers (e.g. ``1.0j``). This is not defined if Python was
  48. built without complex number support.
  49. .. data:: StringType
  50. The type of character strings (e.g. ``'Spam'``); alias of the built-in
  51. :class:`str`.
  52. .. data:: UnicodeType
  53. The type of Unicode character strings (e.g. ``u'Spam'``). This is not defined
  54. if Python was built without Unicode support. It's an alias of the built-in
  55. :class:`unicode`.
  56. .. data:: TupleType
  57. The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
  58. :class:`tuple`.
  59. .. data:: ListType
  60. The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
  61. :class:`list`.
  62. .. data:: DictType
  63. The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the
  64. built-in :class:`dict`.
  65. .. data:: DictionaryType
  66. An alternate name for ``DictType``.
  67. .. data:: FunctionType
  68. LambdaType
  69. The type of user-defined functions and functions created by :keyword:`lambda`
  70. expressions.
  71. .. data:: GeneratorType
  72. The type of :term:`generator`-iterator objects, produced by calling a
  73. generator function.
  74. .. versionadded:: 2.2
  75. .. data:: CodeType
  76. .. index:: builtin: compile
  77. The type for code objects such as returned by :func:`compile`.
  78. .. data:: ClassType
  79. The type of user-defined old-style classes.
  80. .. data:: InstanceType
  81. The type of instances of user-defined classes.
  82. .. data:: MethodType
  83. The type of methods of user-defined class instances.
  84. .. data:: UnboundMethodType
  85. An alternate name for ``MethodType``.
  86. .. data:: BuiltinFunctionType
  87. BuiltinMethodType
  88. The type of built-in functions like :func:`len` or :func:`sys.exit`, and
  89. methods of built-in classes. (Here, the term "built-in" means "written in
  90. C".)
  91. .. data:: ModuleType
  92. The type of modules.
  93. .. data:: FileType
  94. The type of open file objects such as ``sys.stdout``; alias of the built-in
  95. :class:`file`.
  96. .. data:: XRangeType
  97. .. index:: builtin: xrange
  98. The type of range objects returned by :func:`xrange`; alias of the built-in
  99. :class:`xrange`.
  100. .. data:: SliceType
  101. .. index:: builtin: slice
  102. The type of objects returned by :func:`slice`; alias of the built-in
  103. :class:`slice`.
  104. .. data:: EllipsisType
  105. The type of ``Ellipsis``.
  106. .. data:: TracebackType
  107. The type of traceback objects such as found in ``sys.exc_traceback``.
  108. .. data:: FrameType
  109. The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
  110. traceback object.
  111. .. data:: BufferType
  112. .. index:: builtin: buffer
  113. The type of buffer objects created by the :func:`buffer` function.
  114. .. data:: DictProxyType
  115. The type of dict proxies, such as ``TypeType.__dict__``.
  116. .. data:: NotImplementedType
  117. The type of ``NotImplemented``
  118. .. data:: GetSetDescriptorType
  119. The type of objects defined in extension modules with ``PyGetSetDef``, such
  120. as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as
  121. descriptor for object attributes; it has the same purpose as the
  122. :class:`property` type, but for classes defined in extension modules.
  123. .. versionadded:: 2.5
  124. .. data:: MemberDescriptorType
  125. The type of objects defined in extension modules with ``PyMemberDef``, such
  126. as ``datetime.timedelta.days``. This type is used as descriptor for simple C
  127. data members which use standard conversion functions; it has the same purpose
  128. as the :class:`property` type, but for classes defined in extension modules.
  129. In other implementations of Python, this type may be identical to
  130. ``GetSetDescriptorType``.
  131. .. versionadded:: 2.5
  132. .. data:: StringTypes
  133. A sequence containing ``StringType`` and ``UnicodeType`` used to facilitate
  134. easier checking for any string object. Using this is more portable than using a
  135. sequence of the two string types constructed elsewhere since it only contains
  136. ``UnicodeType`` if it has been built in the running version of Python. For
  137. example: ``isinstance(s, types.StringTypes)``.
  138. .. versionadded:: 2.2