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 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:
None
.
True
and False
; this
is an alias of the built-in bool() function.
New in version 2.3.
1
).
1L
).
1.0
).
1.0j
). This is not defined
if Python was built without complex number support.
'Spam'
).
u'Spam'
). This is
not defined if Python was built without Unicode support.
(1, 2, 3, 'Spam')
).
[0, 1, 2, 3]
).
{'Bacon': 1, 'Ham': 0}
).
DictType
.
FunctionType
.
MethodType
.
BuiltinFunction
.
sys.stdout
.
Ellipsis
.
sys.exc_traceback
.
tb.tb_frame
if
tb
is a traceback object.
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)
.
New in version 2.2.