cpython/Doc/library/types.rst

262 lines
6.0 KiB
ReStructuredText
Raw Normal View History

2007-08-15 11:28:01 -03:00
:mod:`types` --- Names for built-in types
=========================================
.. module:: types
:synopsis: Names for built-in types.
This module defines names for some object types that are used by the standard
Python interpreter, but not for the types defined by various extension modules.
Also, it does not include some of the types that arise during processing such as
the ``listiterator`` type. It is safe to use ``from types import *`` --- the
module does not export any names besides the ones listed here. New names
exported by future versions of this module will all end in ``Type``.
Typical use is for functions that do different things depending on their
argument types, like the following::
from types import *
def delete(mylist, item):
if type(item) is IntType:
del mylist[item]
else:
mylist.remove(item)
Starting in Python 2.2, built-in factory functions such as :func:`int` and
:func:`str` are also names for the corresponding types. This is now the
preferred way to access the type instead of using the :mod:`types` module.
Accordingly, the example above should be written as follows::
def delete(mylist, item):
if isinstance(item, int):
del mylist[item]
else:
mylist.remove(item)
The module defines the following names:
.. data:: NoneType
The type of ``None``.
.. data:: TypeType
.. index:: builtin: type
The type of type objects (such as returned by :func:`type`); alias of the
built-in :class:`type`.
.. data:: BooleanType
The type of the :class:`bool` values ``True`` and ``False``; alias of the
built-in :class:`bool`.
.. versionadded:: 2.3
.. data:: IntType
The type of integers (e.g. ``1``); alias of the built-in :class:`int`.
.. data:: LongType
The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`.
.. data:: FloatType
The type of floating point numbers (e.g. ``1.0``); alias of the built-in
:class:`float`.
.. data:: ComplexType
The type of complex numbers (e.g. ``1.0j``). This is not defined if Python was
built without complex number support.
.. data:: StringType
The type of character strings (e.g. ``'Spam'``); alias of the built-in
:class:`str`.
.. data:: UnicodeType
The type of Unicode character strings (e.g. ``u'Spam'``). This is not defined
if Python was built without Unicode support. It's an alias of the built-in
:class:`unicode`.
.. data:: TupleType
The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
:class:`tuple`.
.. data:: ListType
The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
:class:`list`.
.. data:: DictType
The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the
built-in :class:`dict`.
.. data:: DictionaryType
An alternate name for ``DictType``.
.. data:: FunctionType
LambdaType
2007-08-15 11:28:01 -03:00
The type of user-defined functions and functions created by :keyword:`lambda`
expressions.
2007-08-15 11:28:01 -03:00
.. data:: GeneratorType
2007-10-21 07:52:38 -03:00
The type of :term:`generator`-iterator objects, produced by calling a
generator function.
2007-08-15 11:28:01 -03:00
.. versionadded:: 2.2
.. data:: CodeType
.. index:: builtin: compile
The type for code objects such as returned by :func:`compile`.
.. data:: ClassType
2008-08-23 18:40:15 -03:00
The type of user-defined old-style classes.
2007-08-15 11:28:01 -03:00
.. data:: InstanceType
The type of instances of user-defined classes.
.. data:: MethodType
The type of methods of user-defined class instances.
.. data:: UnboundMethodType
An alternate name for ``MethodType``.
.. data:: BuiltinFunctionType
BuiltinMethodType
2007-08-15 11:28:01 -03:00
The type of built-in functions like :func:`len` or :func:`sys.exit`, and
methods of built-in classes. (Here, the term "built-in" means "written in
C".)
2007-08-15 11:28:01 -03:00
.. data:: ModuleType
The type of modules.
.. data:: FileType
The type of open file objects such as ``sys.stdout``; alias of the built-in
:class:`file`.
.. data:: XRangeType
.. index:: builtin: xrange
The type of range objects returned by :func:`xrange`; alias of the built-in
:class:`xrange`.
.. data:: SliceType
.. index:: builtin: slice
The type of objects returned by :func:`slice`; alias of the built-in
:class:`slice`.
.. data:: EllipsisType
The type of ``Ellipsis``.
.. data:: TracebackType
The type of traceback objects such as found in ``sys.exc_traceback``.
.. data:: FrameType
The type of frame objects such as found in ``tb.tb_frame`` if ``tb`` is a
traceback object.
.. data:: BufferType
.. index:: builtin: buffer
The type of buffer objects created by the :func:`buffer` function.
.. data:: DictProxyType
The type of dict proxies, such as ``TypeType.__dict__``.
.. data:: NotImplementedType
The type of ``NotImplemented``
.. data:: GetSetDescriptorType
The type of objects defined in extension modules with ``PyGetSetDef``, such
as ``FrameType.f_locals`` or ``array.array.typecode``. This type is used as
descriptor for object attributes; it has the same purpose as the
:class:`property` type, but for classes defined in extension modules.
2007-08-15 11:28:01 -03:00
.. versionadded:: 2.5
.. data:: MemberDescriptorType
The type of objects defined in extension modules with ``PyMemberDef``, such
as ``datetime.timedelta.days``. This type is used as descriptor for simple C
data members which use standard conversion functions; it has the same purpose
as the :class:`property` type, but for classes defined in extension modules.
Merged revisions 75363,75365,75376,75392,75394,75403,75418,75484,75572,75580,75590,75592,75594-75596,75600,75602-75603,75605-75607,75610-75613,75616-75617,75623,75627,75647 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r75363 | georg.brandl | 2009-10-11 20:31:23 +0200 (So, 11 Okt 2009) | 1 line Add the Python FAQ lists to the documentation. Copied from sandbox/faq. Many thanks to AMK for the preparation work. ........ r75365 | georg.brandl | 2009-10-11 22:16:16 +0200 (So, 11 Okt 2009) | 1 line Fix broken links found by "make linkcheck". scipy.org seems to be done right now, so I could not verify links going there. ........ r75376 | benjamin.peterson | 2009-10-12 03:26:07 +0200 (Mo, 12 Okt 2009) | 1 line platform we don't care about ........ r75392 | andrew.kuchling | 2009-10-13 18:11:49 +0200 (Di, 13 Okt 2009) | 1 line Various link, textual, and markup fixes ........ r75394 | georg.brandl | 2009-10-13 20:10:59 +0200 (Di, 13 Okt 2009) | 1 line Fix markup. ........ r75403 | georg.brandl | 2009-10-14 17:57:46 +0200 (Mi, 14 Okt 2009) | 1 line #7126: os.environ changes *do* take effect in subprocesses started with os.system(). ........ r75418 | georg.brandl | 2009-10-14 20:48:32 +0200 (Mi, 14 Okt 2009) | 1 line #7116: str.join() takes an iterable. ........ r75484 | georg.brandl | 2009-10-18 09:58:12 +0200 (So, 18 Okt 2009) | 1 line Fix missing word. ........ r75572 | benjamin.peterson | 2009-10-20 23:55:17 +0200 (Di, 20 Okt 2009) | 1 line clarify buffer arg #7178 ........ r75580 | georg.brandl | 2009-10-21 09:15:59 +0200 (Mi, 21 Okt 2009) | 1 line #7170: fix explanation about non-weakrefable builtin types. ........ r75590 | benjamin.peterson | 2009-10-22 04:36:47 +0200 (Do, 22 Okt 2009) | 1 line rewrite to be nice to other implementations ........ r75592 | georg.brandl | 2009-10-22 09:05:48 +0200 (Do, 22 Okt 2009) | 1 line Fix punctuation. ........ r75594 | georg.brandl | 2009-10-22 09:56:02 +0200 (Do, 22 Okt 2009) | 1 line Fix markup. ........ r75595 | georg.brandl | 2009-10-22 09:56:56 +0200 (Do, 22 Okt 2009) | 1 line Fix duplicate target. ........ r75596 | georg.brandl | 2009-10-22 10:05:04 +0200 (Do, 22 Okt 2009) | 1 line Add a new directive marking up implementation details and start using it. ........ r75600 | georg.brandl | 2009-10-22 13:01:46 +0200 (Do, 22 Okt 2009) | 1 line Make it more robust. ........ r75602 | georg.brandl | 2009-10-22 13:28:06 +0200 (Do, 22 Okt 2009) | 1 line Document new directive. ........ r75603 | georg.brandl | 2009-10-22 13:28:23 +0200 (Do, 22 Okt 2009) | 1 line Allow short form with text as argument. ........ r75605 | georg.brandl | 2009-10-22 13:48:10 +0200 (Do, 22 Okt 2009) | 1 line Use "impl-detail" directive where applicable. ........ r75606 | georg.brandl | 2009-10-22 17:00:06 +0200 (Do, 22 Okt 2009) | 1 line #6324: membership test tries iteration via __iter__. ........ r75607 | georg.brandl | 2009-10-22 17:04:09 +0200 (Do, 22 Okt 2009) | 1 line #7088: document new functions in signal as Unix-only. ........ r75610 | georg.brandl | 2009-10-22 17:27:24 +0200 (Do, 22 Okt 2009) | 1 line Reorder __slots__ fine print and add a clarification. ........ r75611 | georg.brandl | 2009-10-22 17:42:32 +0200 (Do, 22 Okt 2009) | 1 line #7035: improve docs of the various <method>_errors() functions, and give them docstrings. ........ r75612 | georg.brandl | 2009-10-22 17:52:15 +0200 (Do, 22 Okt 2009) | 1 line #7156: document curses as Unix-only. ........ r75613 | georg.brandl | 2009-10-22 17:54:35 +0200 (Do, 22 Okt 2009) | 1 line #6977: getopt does not support optional option arguments. ........ r75616 | georg.brandl | 2009-10-22 18:17:05 +0200 (Do, 22 Okt 2009) | 1 line Add proper references. ........ r75617 | georg.brandl | 2009-10-22 18:20:55 +0200 (Do, 22 Okt 2009) | 1 line Make printout margin important. ........ r75623 | georg.brandl | 2009-10-23 10:14:44 +0200 (Fr, 23 Okt 2009) | 1 line #7188: fix optionxform() docs. ........ r75627 | fred.drake | 2009-10-23 15:04:51 +0200 (Fr, 23 Okt 2009) | 2 lines add further note about what's passed to optionxform ........ r75647 | georg.brandl | 2009-10-24 12:04:19 +0200 (Sa, 24 Okt 2009) | 1 line Fix markup. ........
2009-10-27 12:08:27 -03:00
.. impl-detail::
In other implementations of Python, this type may be identical to
``GetSetDescriptorType``.
2007-08-15 11:28:01 -03:00
.. versionadded:: 2.5
.. data:: StringTypes
A sequence containing ``StringType`` and ``UnicodeType`` used to facilitate
easier checking for any string object. Using this is more portable than using a
sequence of the two string types constructed elsewhere since it only contains
``UnicodeType`` if it has been built in the running version of Python. For
example: ``isinstance(s, types.StringTypes)``.
.. versionadded:: 2.2