Python类型:types模块

Python类型:types模块 - ArcherXu - 博客园

Python类型:types模块

Python内建函数type(object),用于返回当前object对象的类型。例如:

>>> type(1)
<type 'int'>
>>> type("1")
<type 'str'>

types模块定义了python中所有的类型,包括NoneType, TypeType, ObjectType....

types模块所在的位置为{%PYTHONPATH%\lib\types.py}.

实现方法很简单,把对应的类型赋值给变量:

复制代码
NoneType = type(None)
TypeType = type
ObjectType = object

IntType = int
LongType = long
FloatType = float
BooleanType = bool

StringType = str
复制代码

因此,在我们的代码中可以这样来使用:

>>> import types
>>> type(1)==types.IntType
True
>>> type("1")==types.StringType
True
原文地址:https://www.cnblogs.com/lexus/p/2843571.html