types — Names for built-in types¶
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 ofNone.
- types.TypeType¶
The type of type objects (such as returned bytype()); alias of thebuilt-intype.
- types.BooleanType¶
The type of thebool valuesTrue andFalse; alias of thebuilt-inbool.
New in version 2.3.
- types.IntType¶
- The type of integers (e.g.1); alias of the built-inint.
- types.LongType¶
- The type of long integers (e.g.1L); alias of the built-inlong.
- types.FloatType¶
- The type of floating point numbers (e.g.1.0); alias of the built-infloat.
- types.ComplexType¶
- The type of complex numbers (e.g.1.0j). This is not defined if Python wasbuilt without complex number support.
- types.StringType¶
- The type of character strings (e.g.'Spam'); alias of the built-instr.
- 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.TupleType¶
- The type of tuples (e.g.(1,2,3,'Spam')); alias of the built-intuple.
- types.ListType¶
- The type of lists (e.g.[0,1,2,3]); alias of the built-inlist.
- types.DictType¶
- The type of dictionaries (e.g.{'Bacon':1,'Ham':0}); alias of thebuilt-indict.
- types.DictionaryType¶
- An alternate name forDictType.
- types.FunctionType¶
- types.LambdaType¶
- The type of user-defined functions and functions created bylambdaexpressions.
- types.GeneratorType¶
The type ofgenerator-iterator objects, produced by calling agenerator function.
New in version 2.2.
- types.CodeType¶
The type for code objects such as returned bycompile().
- types.ClassType¶
- The type of user-defined old-style classes.
- types.InstanceType¶
- The type of instances of user-defined classes.
- types.MethodType¶
- The type of methods of user-defined class instances.
- types.UnboundMethodType¶
- An alternate name forMethodType.
- types.BuiltinFunctionType¶
- types.BuiltinMethodType¶
- The type of built-in functions likelen() orsys.exit(), andmethods of built-in classes. (Here, the term “built-in” means “written inC”.)
- types.ModuleType¶
- The type of modules.
- types.FileType¶
- The type of open file objects such assys.stdout; alias of the built-infile.
- types.XRangeType¶
The type of range objects returned byxrange(); alias of the built-inxrange.
- types.SliceType¶
The type of objects returned byslice(); alias of the built-inslice.
- types.EllipsisType¶
- The type ofEllipsis.
- types.TracebackType¶
- The type of traceback objects such as found insys.exc_traceback.
- types.FrameType¶
- The type of frame objects such as found intb.tb_frame iftb is atraceback object.
- types.BufferType¶
The type of buffer objects created by thebuffer() function.
- types.DictProxyType¶
- The type of dict proxies, such asTypeType.__dict__.
- types.NotImplementedType¶
- The type ofNotImplemented
- types.GetSetDescriptorType¶
The type of objects defined in extension modules withPyGetSetDef, suchasFrameType.f_locals orarray.array.typecode. This type is used asdescriptor for object attributes; it has the same purpose as theproperty type, but for classes defined in extension modules.
New in version 2.5.
- types.MemberDescriptorType¶
The type of objects defined in extension modules withPyMemberDef, suchasdatetime.timedelta.days. This type is used as descriptor for simple Cdata members which use standard conversion functions; it has the same purposeas theproperty type, but for classes defined in extension modules.In other implementations of Python, this type may be identical toGetSetDescriptorType.
New in version 2.5.
- types.StringTypes¶
A sequence containingStringType andUnicodeType used 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 containsUnicodeType if it has been built in the running version of Python. Forexample:isinstance(s,types.StringTypes).
New in version 2.2.