合理使用python提供的内置函数提供的信息,可以很好的帮助我们编程,这里介绍3个函数:type(),dir(),help()
type(x)
type()可以接收任何东西作为参数――并返回它的数据类型。
整型、字符串、列表、字典、元组、函数、类、模块,甚至类型对象都可以作为参数被 type 函数接受。
-
In [66]: type(2)
-
Out[66]: int
-
-
In [67]: type('ssdgg')
-
Out[67]: str
-
-
In [68]: alist = []
-
-
In [69]: type(alist)
-
Out[69]: list
-
-
In [70]: import sys
-
-
In [71]: type(sys)
-
Out[71]: module
dir()函数使用方法
使用dir()函数可以查看对像内所有属于及方法,在python中任何东西都是对像,一种,一个等,都有自己的属性和方法,除了常用方法外,其它的你不需要全部记住它,交给dir()函数就好了。dir()函数操作方法很简单,只需要把你想要查询和对像添写到( )括号中就可以使用了,它返回包含要查询对象的所有属性名称的列表
-
In [107]: dir([])
-
Out[107]:
-
['__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']
要查看python某个模块可以干什么,先要导入模块,之后用上面讲过的方式查看就可以。比如要查看都可以干什么,可以像下边这样操作:
-
In [108]: import sys
-
-
In [109]: dir(sys)
-
Out[109]:
-
['__displayhook__',
-
'__doc__',
-
'__excepthook__',
-
'__name__',
-
'__package__',
-
'__stderr__',
-
'__stdin__',
-
'__stdout__',
-
'_clear_type_cache',
-
'_current_frames',
-
'_getframe',
-
'api_version',
-
'argv',
-
'builtin_module_names',
-
'byteorder',
-
'call_tracing',
-
'callstats',
-
'copyright',
-
'displayhook',
-
'dont_write_bytecode',
-
'exc_clear',
-
'exc_info',
-
'exc_traceback',
-
'exc_type',
-
'exc_value',
-
'excepthook',
-
'exec_prefix',
-
'executable',
-
'exit',
-
'exitfunc',
-
'flags',
-
'float_info',
-
'getcheckinterval',
-
'getdefaultencoding',
-
'getdlopenflags',
-
'getfilesystemencoding',
-
'getprofile',
-
'getrecursionlimit',
-
'getrefcount',
-
'getsizeof',
-
'gettrace',
-
'hexversion',
-
'last_traceback',
-
'last_type',
-
'last_value',
-
'maxint',
-
'maxsize',
-
'maxunicode',
-
'meta_path',
-
'modules',
-
'path',
-
'path_hooks',
-
'path_importer_cache',
-
'platform',
-
'prefix',
-
'ps1',
-
'ps2',
-
'ps3',
-
'py3kwarning',
-
'setcheckinterval',
-
'setdlopenflags',
-
'setprofile',
-
'setrecursionlimit',
-
'settrace',
-
'stderr',
-
'stdin',
-
'stdout',
-
'subversion',
-
'version',
-
'version_info',
-
'warnoptions']
help()函数的作用
在使用python来编写代码时,会经常使用python自带函数或模块,一些不常用的函数或是模块的用途不是很清楚,这时候就需要用到help函数来查看帮助。
这里要注意下,help()函数是查看函数或模块用途的详细说明,而dir()函数是查看函数或模块内的操作方法都有什么,输出的是方法列表。
help()括号内填写参数,操作方法很简单。例如:
-
In [110]: help('dir')
-
Help on built-in function dir in module __builtin__:
-
-
dir(...)
-
dir([object]) -> list of strings
-
-
If called without an argument, return the names in the current scope.
-
Else, return an alphabetized list of names comprising (some of) the attributes
-
of the given object, and of attributes reachable from it.
-
If the object supplies a method named __dir__, it will be used; otherwise
-
the default dir() logic is used and returns:
-
for a module object: the module's attributes.
-
for a class object: its attributes, and recursively the attributes
-
of its bases.
-
for any other object: its attributes, its class's attributes, and
-
recursively the attributes of its class
使用help函数查看帮助实例:
在写help()函数使用方法时说过,括号中填写参数,那在这里要注意参数的形式:
1.查看一个模块的帮助
-
In [1]: help('sys')
-
Help on built-in module sys:
-
-
NAME
-
sys
-
-
FILE
-
(built-in)
-
-
MODULE DOCS
-
http://docs.python.org/library/sys
-
-
DESCRIPTION
-
This module provides access to some objects used or maintained by the
-
interpreter and to functions that interact strongly with the interpreter.
-
-
Dynamic objects:
2.查看一个数据类型的帮助
-
In [1]: help('str')
-
Help on class str in module __builtin__:
-
-
class str(basestring)
-
| str(object) -> string
-
|
-
| Return a nice string representation of the object.
-
| If the argument is a string, the return value is the same object.
-
|
-
| Method resolution order:
-
| str
-
| basestring
-
| object
-
|
-
| Methods defined here:
-
|
-
| __add__(...)
-
| x.__add__(y) <==> x+y
-
|
-
| __contains__(...)
-
| x.__contains__(y) <==> y in x
-
|
-
| __eq__(...)
-
| x.__eq__(y) <==> x==y
-
-
[2]+ Stopped ipython
-
[root@ftp ~]# ipython
-
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
-
Type "copyright", "credits" or "license" for more information.
-
-
IPython 1.2.1 -- An enhanced Interactive Python.
-
? -> Introduction and overview of IPython's features.
-
%quickref -> Quick reference.
-
help -> Python's own help system.
-
object? -> Details about 'object', use 'object??' for extra details.
返回字符串的方法及详细说明
-
a = [1,2,3]
-
help(a)
-
Help on list object:
-
class list(object)
-
| list() -> new empty list
-
| list(iterable) -> new list initialized from iterable's items
-
|
-
| Methods defined here:
-
|
-
| __add__(...)
-
| x.__add__(y) <==> x+y
-
|
-
| __contains__(...)
-
| x.__contains__(y) <==> y in x
-
|
-
| __delitem__(...)
-
| x.__delitem__(y) <==> del x[y]
-
|
-
| __delslice__(...)
-
| x.__delslice__(i, j) <==> del x[i:j]
-
|
-
| Use of negative indices is not supported.
-
|
-
| __eq__(...)
-
| x.__eq__(y) <==> x==y
-
|
-
| __ge__(...)
-
| x.__ge__(y) <==> x>=y
-
|
-
| __getattribute__(...)
-
| x.__getattribute__('name
这时help(a)则会打开list的操作方法
-
help(a.append)
-
Help on built-in function append:
-
append(...)
-
L.append(object) -- append object to end
阅读(1450) | 评论(0) | 转发(0) |