blob: 02ff3418aa2133af696c4ae2d257e836c5d49a13 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{types} ---
Raymond Hettinger6fe12992003-01-10 09:33:08 +00002 Names for built-in types}
Fred Drake38e5d272000-04-03 20:13:55 +00003
Fred Drake78a6ddb1998-07-24 14:27:22 +00004\declaremodule{standard}{types}
Raymond Hettinger6fe12992003-01-10 09:33:08 +00005\modulesynopsis{Names for built-in types.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00006
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
Neal Norwitz3cb68a22003-01-10 13:52:30 +00008This module defines names for some object types that are used by
Raymond Hettinger6fe12992003-01-10 09:33:08 +00009the standard Python interpreter, but not for the types defined by various
10extension modules. Also, it does not include some of the types that
11arise during processing such the \code{listiterator} type.
12It is safe to use \samp{from types import *} ---
Fred Drake78a6ddb1998-07-24 14:27:22 +000013the module does not export any names besides the ones listed here.
14New names exported by future versions of this module will all end in
15\samp{Type}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000016
Fred Drake78a6ddb1998-07-24 14:27:22 +000017Typical use is for functions that do different things depending on
18their argument types, like the following:
Fred Drake1a855fa1998-03-16 05:23:50 +000019
Fred Drake19479911998-02-13 06:58:54 +000020\begin{verbatim}
Fred Drake78a6ddb1998-07-24 14:27:22 +000021from types import *
Raymond Hettinger6fe12992003-01-10 09:33:08 +000022def delete(mylist, item):
Fred Drake78a6ddb1998-07-24 14:27:22 +000023 if type(item) is IntType:
Raymond Hettinger6fe12992003-01-10 09:33:08 +000024 del mylist[item]
25 else:
26 mylist.remove(item)
27\end{verbatim}
28
29Starting in Python 2.2, built-in factory functions such as
30\function{int()} and \function{str()} are also names for the
31corresponding types. This is now the preferred way to access
32the type instead of using the \module{types} module. Accordingly,
33the example above should be written as follows:
34
35\begin{verbatim}
36def delete(mylist, item):
37 if isinstance(item, int):
Neal Norwitz3cb68a22003-01-10 13:52:30 +000038 del mylist[item]
Fred Drake78a6ddb1998-07-24 14:27:22 +000039 else:
Neal Norwitz3cb68a22003-01-10 13:52:30 +000040 mylist.remove(item)
Fred Drake19479911998-02-13 06:58:54 +000041\end{verbatim}
Fred Drake1a855fa1998-03-16 05:23:50 +000042
Fred Drake78a6ddb1998-07-24 14:27:22 +000043The module defines the following names:
Guido van Rossum17383111994-04-21 10:32:28 +000044
Fred Drake78a6ddb1998-07-24 14:27:22 +000045\begin{datadesc}{NoneType}
46The type of \code{None}.
47\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048
Fred Drake78a6ddb1998-07-24 14:27:22 +000049\begin{datadesc}{TypeType}
50The type of type objects (such as returned by
51\function{type()}\bifuncindex{type}).
52\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053
Fred Drakec5850752002-05-22 02:44:24 +000054\begin{datadesc}{BooleanType}
55The type of the \class{bool} values \code{True} and \code{False}; this
56is an alias of the built-in \function{bool()} function.
57\versionadded{2.3}
58\end{datadesc}
59
Fred Drake78a6ddb1998-07-24 14:27:22 +000060\begin{datadesc}{IntType}
61The type of integers (e.g. \code{1}).
62\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063
Fred Drake78a6ddb1998-07-24 14:27:22 +000064\begin{datadesc}{LongType}
65The type of long integers (e.g. \code{1L}).
66\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067
Fred Drake78a6ddb1998-07-24 14:27:22 +000068\begin{datadesc}{FloatType}
69The type of floating point numbers (e.g. \code{1.0}).
70\end{datadesc}
Guido van Rossum3a0d8501997-06-02 17:18:00 +000071
Guido van Rossum1eb200d1998-07-24 15:01:05 +000072\begin{datadesc}{ComplexType}
Fred Drake5ecb7aa2002-05-21 14:28:22 +000073The type of complex numbers (e.g. \code{1.0j}). This is not defined
74if Python was built without complex number support.
Guido van Rossum1eb200d1998-07-24 15:01:05 +000075\end{datadesc}
76
Fred Drake78a6ddb1998-07-24 14:27:22 +000077\begin{datadesc}{StringType}
78The type of character strings (e.g. \code{'Spam'}).
79\end{datadesc}
Guido van Rossum3a0d8501997-06-02 17:18:00 +000080
Fred Drake0b721162000-04-06 15:05:04 +000081\begin{datadesc}{UnicodeType}
Fred Drake5ecb7aa2002-05-21 14:28:22 +000082The type of Unicode character strings (e.g. \code{u'Spam'}). This is
83not defined if Python was built without Unicode support.
Fred Drake0b721162000-04-06 15:05:04 +000084\end{datadesc}
85
Fred Drake78a6ddb1998-07-24 14:27:22 +000086\begin{datadesc}{TupleType}
87The type of tuples (e.g. \code{(1, 2, 3, 'Spam')}).
88\end{datadesc}
Guido van Rossum97ea1161998-06-30 15:56:23 +000089
Fred Drake78a6ddb1998-07-24 14:27:22 +000090\begin{datadesc}{ListType}
91The type of lists (e.g. \code{[0, 1, 2, 3]}).
92\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000093
Fred Drake78a6ddb1998-07-24 14:27:22 +000094\begin{datadesc}{DictType}
95The type of dictionaries (e.g. \code{\{'Bacon': 1, 'Ham': 0\}}).
96\end{datadesc}
Fred Drakec3e45491998-02-19 20:22:13 +000097
Fred Drake78a6ddb1998-07-24 14:27:22 +000098\begin{datadesc}{DictionaryType}
99An alternate name for \code{DictType}.
100\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000101
Fred Drake78a6ddb1998-07-24 14:27:22 +0000102\begin{datadesc}{FunctionType}
103The type of user-defined functions and lambdas.
104\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000105
Fred Drake78a6ddb1998-07-24 14:27:22 +0000106\begin{datadesc}{LambdaType}
107An alternate name for \code{FunctionType}.
108\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000109
Tim Peters3e7b1a02001-06-25 19:46:25 +0000110\begin{datadesc}{GeneratorType}
111The type of generator-iterator objects, produced by calling a
112generator function.
113\versionadded{2.2}
114\end{datadesc}
115
Fred Drake78a6ddb1998-07-24 14:27:22 +0000116\begin{datadesc}{CodeType}
117The type for code objects such as returned by
118\function{compile()}\bifuncindex{compile}.
119\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000120
Fred Drake78a6ddb1998-07-24 14:27:22 +0000121\begin{datadesc}{ClassType}
122The type of user-defined classes.
123\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124
Fred Drake78a6ddb1998-07-24 14:27:22 +0000125\begin{datadesc}{InstanceType}
126The type of instances of user-defined classes.
127\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000128
Fred Drake78a6ddb1998-07-24 14:27:22 +0000129\begin{datadesc}{MethodType}
130The type of methods of user-defined class instances.
131\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132
Fred Drake78a6ddb1998-07-24 14:27:22 +0000133\begin{datadesc}{UnboundMethodType}
134An alternate name for \code{MethodType}.
135\end{datadesc}
Guido van Rossum6102b511997-05-28 19:32:11 +0000136
Fred Drake78a6ddb1998-07-24 14:27:22 +0000137\begin{datadesc}{BuiltinFunctionType}
138The type of built-in functions like \function{len()} or
139\function{sys.exit()}.
140\end{datadesc}
Barry Warsawdc0f00a1997-10-06 17:50:48 +0000141
Fred Drake78a6ddb1998-07-24 14:27:22 +0000142\begin{datadesc}{BuiltinMethodType}
143An alternate name for \code{BuiltinFunction}.
144\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000145
Fred Drake78a6ddb1998-07-24 14:27:22 +0000146\begin{datadesc}{ModuleType}
147The type of modules.
148\end{datadesc}
Fred Drakec3e45491998-02-19 20:22:13 +0000149
Fred Drake78a6ddb1998-07-24 14:27:22 +0000150\begin{datadesc}{FileType}
151The type of open file objects such as \code{sys.stdout}.
152\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000153
Fred Drake78a6ddb1998-07-24 14:27:22 +0000154\begin{datadesc}{XRangeType}
155The type of range objects returned by
156\function{xrange()}\bifuncindex{xrange}.
157\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000158
Guido van Rossum1eb200d1998-07-24 15:01:05 +0000159\begin{datadesc}{SliceType}
160The type of objects returned by
161\function{slice()}\bifuncindex{slice}.
162\end{datadesc}
163
164\begin{datadesc}{EllipsisType}
165The type of \code{Ellipsis}.
166\end{datadesc}
167
Fred Drake78a6ddb1998-07-24 14:27:22 +0000168\begin{datadesc}{TracebackType}
169The type of traceback objects such as found in
170\code{sys.exc_traceback}.
171\end{datadesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000172
Fred Drake78a6ddb1998-07-24 14:27:22 +0000173\begin{datadesc}{FrameType}
174The type of frame objects such as found in \code{tb.tb_frame} if
175\code{tb} is a traceback object.
176\end{datadesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000177
178\begin{datadesc}{BufferType}
179The type of buffer objects created by the
180\function{buffer()}\bifuncindex{buffer} function.
181\end{datadesc}
Skip Montanaroc7ba0c42001-09-29 13:49:41 +0000182
183\begin{datadesc}{StringTypes}
Fred Drake5ecb7aa2002-05-21 14:28:22 +0000184A sequence containing \code{StringType} and \code{UnicodeType} used to
185facilitate easier checking for any string object. Using this is more
186portable than using a sequence of the two string types constructed
187elsewhere since it only contains \code{UnicodeType} if it has been
188built in the running version of Python. For example:
189\code{isinstance(s, types.StringTypes)}.
Fred Drakec5850752002-05-22 02:44:24 +0000190\versionadded{2.2}
Skip Montanaroc7ba0c42001-09-29 13:49:41 +0000191\end{datadesc}