学会使用Python的帮助

By | 2019年5月16日

常用的几种方式:

写在前面,个人最喜欢的一个功能是pycharm中的documentation内置显示(默认快捷键为Ctrl+Q),选中函数,Ctrl+Q如下:

也有External documetation,快捷键为Shift+F1

1. dir()主要用来查看对象的属性。
>>> a = [1,2,3]
>>> b = (1,2,3)
>>> print dir(a)
[..... 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> print dir(b)
[.... 'count', 'index']
2. __doc__看属性文档
>>> a = [1,2,3]
>>> a.reverse.__doc__
'L.reverse() -- reverse *IN PLACE*'
3. help()信息比较详细
>>> 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:.....

 |  append(...)
 |      L.append(object) -- append object to end
 |  
 |  count(...)
 |      L.count(value) -> integer -- return number of occurrences of value
 |  
 |  extend(...)
 |      L.extend(iterable) -- extend list by appending elements from the iterable
 |  
 |  index(...)
 |      L.index(value, [start, [stop]]) -> integer -- return first index of value.
 |      Raises ValueError if the value is not present.
 |  
 |  insert(...)
 |      L.insert(index, object) -- insert object before index
 |  
 |  pop(...)
 |      L.pop([index]) -> item -- remove and return item at index (default last).
 |      Raises IndexError if list is empty or index is out of range.
 |  
 |  remove(...)
 |      L.remove(value) -- remove first occurrence of value.
 |      Raises ValueError if the value is not present.
 |  
 |  reverse(...)
 |      L.reverse() -- reverse *IN PLACE*
 |  
 |  sort(...)
 |      L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
 |      cmp(x, y) -> -1, 0, 1
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

发表评论