介绍下,查看函数或模块用途的几个函数
1.help()
作用: 用于查看函数或模块用途的详细说明
语法: help(obj)
返回值: 返回对象的帮助信息
# help('sys') # 查看 sys 模块的帮助 # help('str') a=[1,2,3] #help (a) # 查看列表 list 帮助信息 help (a.append) # 显示list的append方法的帮助
Help on built-in function append: append(...) L.append(object) -- append object to end2.dir()
作用:dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。
语法:dir(obj)
返回值:返回模块的属性列表
print dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']3. type()
作用:只有第一个参数则返回对象的类型,三个参数返回新的类型对象。
语法: class type(name ,bases,dict)
name-类名
bases--元组
dict--字典,类内定义的命名空间变量
返回值:返回参数对象类型
python 的所有数据类型都是类,可以通过 type() 查看该变量的数据类型:
print type(1) print type('aaaa') print type([2]) print type({'aa':10})
<type 'int'> <type 'str'> <type 'list'> <type 'dict'>