Python help()函数用法详解

yipeiwu_com6年前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 Sympy计算梯度、散度和旋度的实例

sympy有个vector 模块,里面提供了求解标量场、向量场的梯度、散度、旋度等计算,官方参考连接: http://docs.sympy.org/latest/modules/vect...

Python中文件的写入读取以及附加文字方法

今天学习到python的读取文件部分。 还是以一段代码为例: filename='programming.txt' with open(filename,'w') as file_o...

Django Channels 实现点对点实时聊天和消息推送功能

简介在很多实际的项目开发中,我们需要实现很多实时功能;而在这篇文章中,我们就利用django channels简单地实现了点对点聊天和消息推送功能。 手边有一个项目需要用到后台消息推送...

Python六大开源框架对比

Python六大开源框架对比

Python 是一门动态、面向对象语言。其最初就是作为一门面向对象语言设计的,并且在后期又加入了一些更高级的特性。除了语言本身的设计目的之外,Python标准 库也是值得大家称赞的,Py...

Python 的 Socket 编程

Socket是网络应用的基础。而Python使得网络socket编程入门变得超级简单。在这篇简介里面我们将创建一个简单服务器,用于接受和相应客户端程序的请求。 由于本人最近对 Linux...