Python help()函数用法详解

yipeiwu_com5年前Python基础

help函数是python的一个内置函数(python的内置函数可以直接调用,无需import),它是python自带的函数,任何时候都可以被使用。help函数能作什么、怎么使用help函数查看python模块中函数的用法,和使用help函数时需要注意哪些问题,下面来简单的说一下。

一、help()函数的作用
在使用python来编写代码时,会经常使用python自带函数或模块,一些不常用的函数或是模块的用途不是很清楚,这时候就需要用到help函数来查看帮助。
这里要注意下,help()函数是查看函数或模块用途的详细说明,而dir()函数是查看函数或模块内的操作方法都有什么,输出的是方法列表。
二、怎么使用help函数查看python模块中函数的用法
help()括号内填写参数,操作方法很简单。例如:

复制代码 代码如下:
>>> help('dir')
Help on built-in function dir in module builtins:

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 attribut
es
    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's base classes.

三、使用help函数查看帮助实例

在写help()函数使用方法时说过,括号中填写参数,那在这里要注意参数的形式:

1、查看一个模块的帮助

复制代码 代码如下:
>>>help('sys')

之后它回打开这个模块的帮助文档
2、查看一个数据类型的帮助
复制代码 代码如下:
>>>help('str')

返回字符串的方法及详细说明
复制代码 代码如下:
>>>a = [1,2,3]
>>>help(a)

这时help(a)则会打开list的操作方法
复制代码 代码如下:
>>>help(a.append)

会显示list的append方法的帮助

相关文章

Python求两个list的差集、交集与并集的方法

本文实例讲述了Python求两个list的差集、交集与并集的方法。分享给大家供大家参考。具体如下: list就是指两个数组之间的差集,交集,并集了,这个小学数学时就学过的东西,下面就以实...

python opencv 批量改变图片的尺寸大小的方法

python opencv 批量改变图片的尺寸大小的方法

我目标文件夹下有一大批图片,我要把它转变为指定尺寸大小的图片,用pthon和opencv实现的。 以上为原图片。 import cv2 import os # 按指定图像大小调整尺...

Python数据结构与算法之常见的分配排序法示例【桶排序与基数排序】

本文实例讲述了Python数据结构与算法之常见的分配排序法。分享给大家供大家参考,具体如下: 箱排序(桶排序) 箱排序是根据关键字的取值范围1~m,预先建立m个箱子,箱排序要求关键字类型...

python实现下载指定网址所有图片的方法

本文实例讲述了python实现下载指定网址所有图片的方法。分享给大家供大家参考。具体实现方法如下: #coding=utf-8 #download pictures of the u...

python错误处理详解

在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因。在操作系统提供的调用中,返回错误码非常常见。比如打开文件的函数open(),成...