8.15.types — Names for built-in types¶
Source code:Lib/types.py
This module defines names for some object types that are used by the standardPython 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 asthelistiterator type. It is safe to usefromtypesimport* — themodule does not export any names besides the ones listed here. New namesexported by future versions of this module will all end inType.
Typical use is for functions that do different things depending on theirargument types, like the following:
fromtypesimport*defdelete(mylist,item):iftype(item)isIntType:delmylist[item]else:mylist.remove(item)
Starting in Python 2.2, built-in factory functions such asint() andstr() are also names for the corresponding types. This is now thepreferred way to access the type instead of using thetypes module.Accordingly, the example above should be written as follows:
defdelete(mylist,item):ifisinstance(item,int):delmylist[item]else:mylist.remove(item)
The module defines the following names:
types.NoneType¶The type of
None.
types.BooleanType¶The type of the
boolvaluesTrueandFalse; alias of thebuilt-inbool.New in version 2.3.
types.ComplexType¶The type of complex numbers (e.g.
1.0j). This is not defined if Python wasbuilt without complex number support.
types.UnicodeType¶The type of Unicode character strings (e.g.
u'Spam'). This is not definedif Python was built without Unicode support. It’s an alias of the built-inunicode.
types.ListType¶The type of lists (e.g.
[0,1,2,3]); alias of the built-inlist.
types.DictionaryType¶An alternate name for
DictType.
types.FunctionType¶types.LambdaType¶The type of user-defined functions and functions created by
lambdaexpressions.
types.GeneratorType¶The type ofgenerator-iterator objects, produced by calling agenerator function.
New in version 2.2.
types.ClassType¶The type of user-defined old-style classes.
types.InstanceType¶The type of instances of user-defined old-style classes.
types.MethodType¶The type of methods of user-defined class instances.
types.UnboundMethodType¶An alternate name for
MethodType.
types.BuiltinFunctionType¶types.BuiltinMethodType¶The type of built-in functions like
len()orsys.exit(), andmethods of built-in classes. (Here, the term “built-in” means “written inC”.)
types.ModuleType¶The type of modules.
types.EllipsisType¶The type of
Ellipsis.
types.TracebackType¶The type of traceback objects such as found in
sys.exc_traceback.
types.FrameType¶The type of frame objects such as found in
tb.tb_frameiftbis atraceback object.
types.DictProxyType¶The type of dict proxies, such as
TypeType.__dict__.
types.NotImplementedType¶The type of
NotImplemented
types.GetSetDescriptorType¶The type of objects defined in extension modules with
PyGetSetDef, suchasFrameType.f_localsorarray.array.typecode. This type is used asdescriptor for object attributes; it has the same purpose as thepropertytype, but for classes defined in extension modules.New in version 2.5.
types.MemberDescriptorType¶The type of objects defined in extension modules with
PyMemberDef, suchasdatetime.timedelta.days. This type is used as descriptor for simple Cdata members which use standard conversion functions; it has the same purposeas thepropertytype, but for classes defined in extension modules.CPython implementation detail: In other implementations of Python, this type may be identical to
GetSetDescriptorType.New in version 2.5.
types.StringTypes¶A sequence containing
StringTypeandUnicodeTypeused to facilitateeasier checking for any string object. Using this is more portable than using asequence of the two string types constructed elsewhere since it only containsUnicodeTypeif it has been built in the running version of Python. Forexample:isinstance(s,types.StringTypes).New in version 2.2.
