8.15. types
— Names for built-in types¶
源代码: Lib/types.py
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:
-
types.
NoneType
¶ The type of
None
.
-
types.
BooleanType
¶ The type of the
bool
valuesTrue
andFalse
; alias of the built-inbool
.2.3 新版功能.
-
types.
ComplexType
¶ The type of complex numbers (e.g.
1.0j
). This is not defined if Python was built without complex number support.
-
types.
UnicodeType
¶ The type of Unicode character strings (e.g.
u'Spam'
). This is not defined if 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
lambda
expressions.
-
types.
GeneratorType
¶ The type of generator-iterator objects, produced by calling a generator function.
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
¶ 用户自定义类实例方法的类型。
-
types.
UnboundMethodType
¶ An alternate name for
MethodType
.
-
types.
BuiltinFunctionType
¶ -
types.
BuiltinMethodType
¶ 内置函数例如
len()
或sys.exit()
以及内置类方法的类型。 (这里所说的“内置”是指“以 C 语言编写”。)
-
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
¶ 帧对象的类型,例如
tb.tb_frame
中的对象,其中tb
是一个回溯对象。
-
types.
DictProxyType
¶ The type of dict proxies, such as
TypeType.__dict__
.
-
types.
NotImplementedType
¶ The type of
NotImplemented
-
types.
GetSetDescriptorType
¶ 使用
PyGetSetDef
在扩展模块中定义的对象的类型,例如FrameType.f_locals
或array.array.typecode
。 此类型被用作对象属性的描述器;它的目的与property
类型相同,但专门针对在扩展模块中定义的类。2.5 新版功能.
-
types.
MemberDescriptorType
¶ 使用
PyMemberDef
在扩展模块中定义的对象的类型,例如datetime.timedelta.days
。 此类型被用作使用标准转换函数的简单 C 数据成员的描述器;它的目的与property
类型相同,但专门针对在扩展模块中定义的类。在 Python 的其它实现中,此类型可能与
GetSetDescriptorType
完全相同。2.5 新版功能.
-
types.
StringTypes
¶ 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)
.2.2 新版功能.