8.15. 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:

类型。 NoneType

类型对于 None .

类型。 TypeType

The type of type objects (such as returned by type() ); alias of the built-in type .

类型。 BooleanType

The type of the bool True and False ; alias of the built-in bool .

2.3 版新增。

类型。 IntType

The type of integers (e.g. 1 ); alias of the built-in int .

类型。 LongType

The type of long integers (e.g. 1L ); alias of the built-in long .

类型。 FloatType

The type of floating point numbers (e.g. 1.0 ); alias of the built-in float .

类型。 ComplexType

The type of complex numbers (e.g. 1.0j ). This is not defined if Python was built without complex number support.

类型。 StringType

The type of character strings (e.g. 'Spam' ); alias of the built-in str .

类型。 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-in unicode .

类型。 TupleType

The type of tuples (e.g. (1, 2, 3, 'Spam') ); alias of the built-in tuple .

类型。 ListType

The type of lists (e.g. [0, 1, 2, 3] ); alias of the built-in list .

类型。 DictType

The type of dictionaries (e.g. {'Bacon': 1, 'Ham': 0} ); alias of the built-in dict .

类型。 DictionaryType

An alternate name for DictType .

类型。 FunctionType
类型。 LambdaType

用户定义函数和函数的类型,创建通过 lambda 表达式。

类型。 GeneratorType

类型对于 generator -iterator objects, produced by calling a generator function.

2.2 版新增。

类型。 CodeType

The type for code objects such as returned by compile() .

类型。 ClassType

The type of user-defined old-style classes.

类型。 InstanceType

The type of instances of user-defined old-style classes.

类型。 MethodType

用户定义类实例方法的类型。

类型。 UnboundMethodType

An alternate name for MethodType .

类型。 BuiltinFunctionType
类型。 BuiltinMethodType

内置函数的类型像 len() or sys.exit() , and methods of built-in classes. (Here, the term “built-in” means “written in C”.)

类型。 ModuleType

The type of modules.

类型。 FileType

The type of open file objects such as sys.stdout ; alias of the built-in file .

类型。 XRangeType

The type of range objects returned by xrange() ; alias of the built-in xrange .

类型。 SliceType

The type of objects returned by slice() ; alias of the built-in slice .

类型。 EllipsisType

类型对于 Ellipsis .

类型。 TracebackType

The type of traceback objects such as found in sys.exc_traceback .

类型。 FrameType

The type of frame objects such as found in tb.tb_frame if tb is a traceback object.

类型。 BufferType

The type of buffer objects created by the buffer() 函数。

类型。 DictProxyType

The type of dict proxies, such as TypeType.__dict__ .

类型。 NotImplementedType

类型对于 NotImplemented

类型。 GetSetDescriptorType

The type of objects defined in extension modules with PyGetSetDef ,譬如 FrameType.f_locals or array.array.typecode . This type is used as descriptor for object attributes; it has the same purpose as the property type, but for classes defined in extension modules.

2.5 版新增。

类型。 MemberDescriptorType

The type of objects defined in extension modules with PyMemberDef ,譬如 datetime.timedelta.days . This type is used as descriptor for simple C data members which use standard conversion functions; it has the same purpose as the property type, but for classes defined in extension modules.

CPython 实现细节: In other implementations of Python, this type may be identical to GetSetDescriptorType .

2.5 版新增。

类型。 StringTypes

A sequence containing 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) .

2.2 版新增。

上一话题

8.12. UserDict — 字典对象的类包裹器

下一话题

8.16. new — 创建运行时内部对象

本页