5.15 types -- 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 int() and str() are also names for the corresponding types. This is now the preferred way to access the type instead of using the 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:
-
The type of
None
.
-
The type of the bool values
True
andFalse
; this is an alias of the built-in bool() function. New in version 2.3.
-
The type of integers (e.g.
1
).
-
The type of long integers (e.g.
1L
).
-
The type of floating point numbers (e.g.
1.0
).
-
The type of complex numbers (e.g.
1.0j
). This is not defined if Python was built without complex number support.
-
The type of character strings (e.g.
'Spam'
).
-
The type of Unicode character strings (e.g.
u'Spam'
). This is not defined if Python was built without Unicode support.
-
The type of tuples (e.g.
(1, 2, 3, 'Spam')
).
-
The type of lists (e.g.
[0, 1, 2, 3]
).
-
The type of dictionaries (e.g.
{'Bacon': 1, 'Ham': 0}
).
-
An alternate name for
DictType
.
- The type of user-defined functions and lambdas.
-
An alternate name for
FunctionType
.
- The type of generator-iterator objects, produced by calling a generator function. New in version 2.2.
- The type of user-defined classes.
- The type of instances of user-defined classes.
- The type of methods of user-defined class instances.
-
An alternate name for
MethodType
.
- The type of built-in functions like len() or sys.exit().
-
An alternate name for
BuiltinFunction
.
- The type of modules.
-
The type of open file objects such as
sys.stdout
.
-
The type of
Ellipsis
.
-
The type of traceback objects such as found in
sys.exc_traceback
.
-
The type of frame objects such as found in
tb.tb_frame
iftb
is a traceback object.
-
The type of dict proxies, such as
TypeType.__dict__
.
-
The type of
NotImplemented
-
The type of objects defined in extension modules with
PyGetSetDef
, such asFrameType.f_locals
orarray.array.typecode
. This constant is not defined in implementations of Python that do not have such extension types, so for portable code usehasattr(types, 'GetSetDescriptorType')
. New in version 2.5.
-
The type of objects defined in extension modules with
PyMemberDef
, such asdatetime.timedelta.days
. This constant is not defined in implementations of Python that do not have such extension types, so for portable code usehasattr(types, 'MemberDescriptorType')
. New in version 2.5.
-
A sequence containing
StringType
andUnicodeType
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 containsUnicodeType
if it has been built in the running version of Python. For example:isinstance(s, types.StringTypes)
. New in version 2.2.