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

 

posted on 2012-12-13 20:14  ArcherXu  阅读(4663)  评论(0编辑  收藏  举报

导航