我们可以很容易的通过Python解释器获取帮助。如果想知道一个对象(object)更多的信息,那么可以调用help(object)!另外还有一些有用的方法,dir(object)会显示该对象的大部分相关属性名,还有object._ doc _会显示其相对应的文档字符串。下面对其进行逐一介绍。

1、 help()

help函数是Python的一个内置函数。
函数原型:help([object])。
可以帮助我们了解该对象的更多信息。
If no argument is given, the interactive help system starts on the interpreter console.

>>> help()

Welcome to Python 2.7!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/. Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit". To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam". help> int # 由于篇幅问题,此处只显示部分内容,下同
Help on class int in module __builtin__: class int(object)
| int(x=0) -> int or long
| int(x, base=10) -> int or long
| ..... help>

If the argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is generated.

>>> help(abs)  # 查看abs函数
Help on built-in function abs in module __builtin__: abs(...)
abs(number) -> number Return the absolute value of the argument. >>> help(math) # 查看math模块,此处只显示部分内容
Help on built-in module math: NAME
math FILE
(built-in) DESCRIPTION
This module is always available. It provides access to the
mathematical functions defined by the C standard. FUNCTIONS
acos(...)
acos(x) Return the arc cosine (measured in radians) of x. ..... >>>

2、dir()

dir函数是Python的一个内置函数。
函数原型:dir([object])
可以帮助我们获取该对象的大部分相关属性。
Without arguments, return the list of names in the current local scope.

>>> dir()  # 没有参数
['__builtins__', '__doc__', '__name__', '__package__']
>>>
>>> import math # 引入一个包和一个变量,再次dir()
>>> a=3
>>>
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'math']
>>>

With an argument, attempt to return a list of valid attributes for that object.

>>> import math
>>> dir(math) # math模块作为参数
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>>

The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:
• If the object is a module object, the list contains the names of the module’s attributes.

>>> import math
>>> dir(math) # math模块作为参数
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>>

• If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.

>>> dir(float)  # 类型
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
>>> dir(3.4)
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
>>>
>>> class A:
x=3
y=4 >>> class B(A):
z=5 >>> dir(B) # 类
['__doc__', '__module__', 'x', 'y', 'z']
>>>

• Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.

3、_ doc_

在Python中有一个奇妙的特性,文档字符串,又称为DocStrings。
用它可以为我们的模块、类、函数等添加说明性的文字,使程序易读易懂,更重要的是可以通过Python自带的标准方法将这些描述性文字信息输出。
上面提到的自带的标准方法就是_ doc _。前后各两个下划线。
:当不是函数、方法、模块等调用doc时,而是具体对象调用时,会显示此对象从属的类型的构造函数的文档字符串。

>>> import math
>>> math.__doc__ # 模块
'This module is always available. It provides access to the\nmathematical functions defined by the C standard.'
>>> abs.__doc__ # 内置函数
'abs(number) -> number\n\nReturn the absolute value of the argument.'
>>> def addxy(x,y):
'''the sum of x and y'''
return x+y >>> addxy.__doc__ # 自定义函数
'the sum of x and y'
>>> a=[1,2,4]
>>> a.count.__doc__ # 方法
'L.count(value) -> integer -- return number of occurrences of value'
>>> b=3
>>> b.__doc__ # 具体的对象
"int(x=0) -> int or long\nint(x, base=10) -> int or long\n\nConvert a number or string to an integer, or return 0 if no arguments\nare given. If x is floating point, the conversion truncates towards zero.\nIf x is outside the integer range, the function returns a long instead.\n\nIf x is not a number or if base is given, then x must be a string or\nUnicode object representing an integer literal in the given base. The\nliteral can be preceded by '+' or '-' and be surrounded by whitespace.\nThe base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to\ninterpret the base from the string as an integer literal.\n>>> int('0b100', base=0)\n4"
>>>

其实我们可以通过一定的手段来查看这些文档字符串,比如使用Pycharm,在对应的模块、函数、方法等上鼠标“右击”->Go to->Declaration。例如:查看内置函数abs的文档字符串

我们再举一个具体的对象的例子,例如,上面具体的整型对象b的doc显示的就是其所从属的int类型的文档字符串:

参考文献:
1、Python帮助文档

出处:http://blog.csdn.net/dq_dm/article/details/45672623

Python获取帮助的3种方式(转载)的更多相关文章

  1. Python 操作 MySQL 的5种方式(转)

    Python 操作 MySQL 的5种方式 不管你是做数据分析,还是网络爬虫,Web 开发.亦或是机器学习,你都离不开要和数据库打交道,而 MySQL 又是最流行的一种数据库,这篇文章介绍 Pytho ...

  2. Python实现屏幕截图的两种方式

    Python实现屏幕截图的两种方式 使用windows API 使用PIL中的ImageGrab模块 下面对两者的特点和用法进行详细解释. 一.Python调用windows API实现屏幕截图 好处 ...

  3. Python 配置日志的几种方式

    Python配置日志的几种方式 作为开发者,我们可以通过以下3种方式来配置logging: (1)使用Python代码显式的创建loggers,handlers和formatters并分别调用它们的配 ...

  4. 横向对比分析Python解析XML的四种方式

    横向对比分析Python解析XML的四种方式 在最初学习PYTHON的时候,只知道有DOM和SAX两种解析方法,但是其效率都不够理想,由于需要处理的文件数量太大,这两种方式耗时太高无法接受. 在网络搜 ...

  5. 最全总结!聊聊 Python 调用 JS 的几种方式

    1. 前言 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大 ...

  6. Python 操作 MySQL 的5种方式

    不管你是做数据分析,还是网络爬虫,Web 开发.亦或是机器学习,你都离不开要和数据库打交道,而 MySQL 又是最流行的一种数据库,这篇文章介绍 Python 操作 MySQL 的5种方式,你可以在实 ...

  7. 获取Type的三种方式

    using System;using UnityEngine; public class Type_Test : MonoBehaviour{    private void Awake()    { ...

  8. python实现单例模式的三种方式及相关知识解释

    python实现单例模式的三种方式及相关知识解释 模块模式 装饰器模式 父类重写new继承 单例模式作为最常用的设计模式,在面试中很可能遇到要求手写.从最近的学习python的经验而言,singlet ...

  9. java动态获取WebService的两种方式(复杂参数类型)

    java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_Fligh ...

随机推荐

  1. [C2W3] Improving Deep Neural Networks : Hyperparameter tuning, Batch Normalization and Programming Frameworks

    第三周:Hyperparameter tuning, Batch Normalization and Programming Frameworks 调试处理(Tuning process) 目前为止, ...

  2. 《anchor-based v.s. anchor-free》

    作者:青青子衿链接:https://www.zhihu.com/question/356551927/answer/926659692来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  3. 学习:反调试之IsDebuggerPresent

    前言:一个反调试IsDebuggerPresent的CreackMe IsDebuggerPresent函数的了解: IsDebuggerPresent 作用 确定调用进程是否由用户模式的调试器调试. ...

  4. python3.8.0 Django 开发后端接口api 部署到 Linux Centos7上

    经历了两天的时候终于把本地使用python3 django开发的接口API部署到服务器上了,还是记录一下,以免之后忘记,哈哈 注意一点,就是,centos7是基于python2的,我这边默认的是pyt ...

  5. 第三方系统平台如何对接gooflow2.0

    第一步,参与者数据源配置 目前提供3种参与者数据源(员工,角色,部门),还有一种sql语句 XML配置如下 <?xml version="1.0" encoding=&quo ...

  6. Codeforces Round #573 (Div. 2) Tokitsukaze and Mahjong 水题

    B. Tokitsukaze and Mahjong time limit per test1 second memory limit per test256 megabytes Tokitsukaz ...

  7. MyEclipse清除所有断点的方法

    今天调试网站时遇到点奇怪的问题,于是在宠大的代码段里加了N处断点,但从其它项目代码段链接代码加入断点后,关闭标签再次打开时发现断点看不到了,但运行到那段代码时依然会被中断.没有断点标记,不能手动取消怎 ...

  8. stacky footer

    div{border:1px solid #CCC;} .wrapper{ width:100%; height:100%; display:flex; flex-flow: column; } .c ...

  9. jvm 性能调优工具之 jmap

    概述 命令jmap是一个多功能的命令.它可以生成 java 程序的 dump 文件, 也可以查看堆内对象示例的统计信息.查看 ClassLoader 的信息以及 finalizer 队列. jmap ...

  10. cocos2dx 3.17(Windows下) 接入skynet和sprotol

    大致流程一致,但是他的github上的版本,没有Windows的版本.打开他的win的工程会提示缺少一个模块. 本人环境 cocos2dx 3.17.1 当前最新 skynet-无视-当前最新 VS2 ...