利用Python3内置文档资源高效学习及官方中文文档

813 阅读3分钟

概述

从前面的对Python基础知识方法介绍中,我们几乎是围绕Python内置方法进行探索实践,比如字符串、列表、字典等数据结构的内置方法,和大量内置的标准库,诸如functoolstimethreading等等,而我们怎么快速学习掌握并学会使用这个Python的工具集呢? 我们可以利用Python的内置文档大量资源既可以掌握许多关于Python工具集的基本使用。

dir函数

Python中内置的dir函数用于提取某对象内所有属性的方法,,诸如对象的方法及属性

L = [1, 2, 3, 4]
print(dir(L))
print([])

示例结果:

['__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']
['__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']

可以看到我们可以传入某实例对象查看其属性,也可以直接传入其内置类型的空对象查看对应属性,我们甚至还可以直接传入类型的名称得到对应的属性列表:

print(dir(list))

示例结果:

['__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']

虽然我们获得了对象的属性,但我们仍然不知道这些属性方法的含义,那么我们可以利用文档字符串帮助我们继续学习对象属性。

文档字符串:doc

文档字符串是由Python自动生成的,而生成的内内容和位置取决于我们的放置方式,文档字符串也是一段注释,放在模块文件、函数以及类语句的顶端,然后Python会自动封装这个字符串,即成为所谓的文档字符串,通过对象的__doc__进行查看。

def two_sum(x, y):
    '''
    Used to calculate the sum of two numbers
    '''
    return x + y


print(two_sum.__doc__)

示例结果:

Used to calculate the sum of two numbers

以上示例就实现了对一个函数(用于计算两数之和)绑定文档字符串并查看其文档字符串的过程。我们也可以查看一些内置类型的某属性的具体使用方法,比如查看列表对象中pop的具体含义和用法

L = [1, 2, 3, 4]
print(L.pop.__doc__)

示例结果:

L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.

PyDoc:help函数

我们可以利用Python中help函数工具更加友好结构化的展示对象的文档字符串和其他的信息,对于对于某些较大的对象help内容会分成几段,甚至可以进行交互展示对象的详细信息。

help(list)

交互结果:

Help on class list in module __builtin__:

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]
 |
-- More  --

比如我们可以通过help查看列表的所有详细信息和属性的用法等,通过回车键查看更多的信息。

官方中文文档

对于英文阅读有一定困难的小伙伴,新出Python官方中文文档是较好的学习体验教程:docs.python.org/zh-cn/3/,从入门教程,标准库,在到Python高级特性应有尽有,算是不错的学习资源和一本常用的**“Python字典”**。