summaryrefslogtreecommitdiffstats
path: root/Doc/lib/libtypes.tex
blob: 5e0c5a6ab523a1c7bc4c57788a82d34ce0d0011a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
\section{\module{types} ---
         Names for built-in types}

\declaremodule{standard}{types}
\modulesynopsis{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 \code{listiterator} type.
It is safe to use \samp{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
\samp{Type}.

Typical use is for functions that do different things depending on
their argument types, like the following:

\begin{verbatim}
from types import *
def delete(mylist, item):
    if type(item) is IntType:
       del mylist[item]
    else:
       mylist.remove(item)
\end{verbatim}

Starting in Python 2.2, built-in factory functions such as
\function{int()} and \function{str()} are also names for the
corresponding types.  This is now the preferred way to access
the type instead of using the \module{types} module.  Accordingly,
the example above should be written as follows:

\begin{verbatim}
def delete(mylist, item):
    if isinstance(item, int):
       del mylist[item]
    else:
       mylist.remove(item)
\end{verbatim}

The module defines the following names:

\begin{datadesc}{NoneType}
The type of \code{None}.
\end{datadesc}

\begin{datadesc}{TypeType}
The type of type objects (such as returned by
\function{type()}\bifuncindex{type}).
\end{datadesc}

\begin{datadesc}{BooleanType}
The type of the \class{bool} values \code{True} and \code{False}; this
is an alias of the built-in \function{bool()} function.
\versionadded{2.3}
\end{datadesc}

\begin{datadesc}{IntType}
The type of integers (e.g. \code{1}).
\end{datadesc}

\begin{datadesc}{LongType}
The type of long integers (e.g. \code{1L}).
\end{datadesc}

\begin{datadesc}{FloatType}
The type of floating point numbers (e.g. \code{1.0}).
\end{datadesc}

\begin{datadesc}{ComplexType}
The type of complex numbers (e.g. \code{1.0j}).  This is not defined
if Python was built without complex number support.
\end{datadesc}

\begin{datadesc}{StringType}
The type of character strings (e.g. \code{'Spam'}).
\end{datadesc}

\begin{datadesc}{UnicodeType}
The type of Unicode character strings (e.g. \code{u'Spam'}).  This is
not defined if Python was built without Unicode support.
\end{datadesc}

\begin{datadesc}{TupleType}
The type of tuples (e.g. \code{(1, 2, 3, 'Spam')}).
\end{datadesc}

\begin{datadesc}{ListType}
The type of lists (e.g. \code{[0, 1, 2, 3]}).
\end{datadesc}

\begin{datadesc}{DictType}
The type of dictionaries (e.g. \code{\{'Bacon': 1, 'Ham': 0\}}).
\end{datadesc}

\begin{datadesc}{DictionaryType}
An alternate name for \code{DictType}.
\end{datadesc}

\begin{datadesc}{FunctionType}
The type of user-defined functions and lambdas.
\end{datadesc}

\begin{datadesc}{LambdaType}
An alternate name for \code{FunctionType}.
\end{datadesc}

\begin{datadesc}{GeneratorType}
The type of generator-iterator objects, produced by calling a
generator function.
\versionadded{2.2}
\end{datadesc}

\begin{datadesc}{CodeType}
The type for code objects such as returned by
\function{compile()}\bifuncindex{compile}.
\end{datadesc}

\begin{datadesc}{ClassType}
The type of user-defined classes.
\end{datadesc}

\begin{datadesc}{InstanceType}
The type of instances of user-defined classes.
\end{datadesc}

\begin{datadesc}{MethodType}
The type of methods of user-defined class instances.
\end{datadesc}

\begin{datadesc}{UnboundMethodType}
An alternate name for \code{MethodType}.
\end{datadesc}

\begin{datadesc}{BuiltinFunctionType}
The type of built-in functions like \function{len()} or
\function{sys.exit()}.
\end{datadesc}

\begin{datadesc}{BuiltinMethodType}
An alternate name for \code{BuiltinFunction}.
\end{datadesc}

\begin{datadesc}{ModuleType}
The type of modules.
\end{datadesc}

\begin{datadesc}{FileType}
The type of open file objects such as \code{sys.stdout}.
\end{datadesc}

\begin{datadesc}{XRangeType}
The type of range objects returned by
\function{xrange()}\bifuncindex{xrange}.
\end{datadesc}

\begin{datadesc}{SliceType}
The type of objects returned by
\function{slice()}\bifuncindex{slice}.
\end{datadesc}

\begin{datadesc}{EllipsisType}
The type of \code{Ellipsis}.
\end{datadesc}

\begin{datadesc}{TracebackType}
The type of traceback objects such as found in
\code{sys.exc_traceback}.
\end{datadesc}

\begin{datadesc}{FrameType}
The type of frame objects such as found in \code{tb.tb_frame} if
\code{tb} is a traceback object.
\end{datadesc}

\begin{datadesc}{BufferType}
The type of buffer objects created by the
\function{buffer()}\bifuncindex{buffer} function.
\end{datadesc}

\begin{datadesc}{DictProxyType}
The type of dict proxies, such as \code{TypeType.__dict__}.
\end{datadesc}

\begin{datadesc}{NotImplementedType}
The type of \code{NotImplemented}
\end{datadesc}

\begin{datadesc}{GetSetDescriptorType}
The type of objects defined in extension modules with \code{PyGetSetDef}, such
as \code{FrameType.f_locals} or \code{array.array.typecode}.  This constant is
not defined in implementations of Python that do not have such extension
types, so for portable code use \code{hasattr(types, 'GetSetDescriptorType')}.
\versionadded{2.5}
\end{datadesc}

\begin{datadesc}{MemberDescriptorType}
The type of objects defined in extension modules with \code{PyMemberDef}, such
as \code {datetime.timedelta.days}.  This constant is not defined in
implementations of Python that do not have such extension types, so for
portable code use \code{hasattr(types, 'MemberDescriptorType')}.
\versionadded{2.5}
\end{datadesc}

\begin{datadesc}{StringTypes}
A sequence containing \code{StringType} and \code{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 \code{UnicodeType} if it has been
built in the running version of Python.  For example:
\code{isinstance(s, types.StringTypes)}.
\versionadded{2.2}
\end{datadesc}