IPython功能(1)IPython帮助

方法1,python内置help()

In [1]: help(len)
Help on built-in function len in module builtins:

len(...)
    len(object) -> integer

    Return the number of items of a sequence or mapping.

方法2,简化的问号?

In [2]: len?
Type:        builtin_function_or_method
String form: <built-in function len>
Namespace:   Python builtin
Docstring:
len(object) -> integer

Return the number of items of a sequence or mapping.

也可以对object的方法或object本身使用问号
object的方法:

In [3]: L = [1, 2, 3]
In [4]: L.insert?
Type:        builtin_function_or_method
String form: <built-in method insert of list object at 0x1024b8ea8>
Docstring:   L.insert(index, object) -- insert object before index

object本身:

In [5]: L?
Type:        list
String form: [1, 2, 3]
Length:      3
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items

也就是说,可以对function使用问号:

In [6]: def square(a):
  ....:     """Return the square of a."""
  ....:     return a ** 2
  ....:
In [7]: square?
Type:        function
String form: <function square at 0x103713cb0>
Definition:  square(a)
Docstring:   Return the square of a.

连用2个问号??可以拿到function的源代码:

In [8]: square??
Type:        function
String form: <function square at 0x103713cb0>
Definition:  square(a)
Source:
def square(a):
    "Return the square of a"
    return a ** 2

但是python内置函数后面跟??很可能只得到跟?一样的结果:

In [9]: len??
Type:        builtin_function_or_method
String form: <built-in function len>
Namespace:   Python builtin
Docstring:
len(object) -> integer

Return the number of items of a sequence or mapping.

在对象后面加.<TAB>可以获得这个对象的属性:

In [10]: L.<TAB>
L.append   L.copy     L.extend   L.insert   L.remove   L.sort     
L.clear    L.count    L.index    L.pop      L.reverse

可以再加字符缩小范围:

In [10]: L.c<TAB>
L.clear  L.copy   L.count  

In [10]: L.co<TAB>
L.copy   L.count

在对象后面加._<TAB>可以看到对象的隐藏属性:

In [10]: L._<TAB>
L.__add__           L.__gt__            L.__reduce__
L.__class__         L.__hash__          L.__reduce_ex__

Tab填补:知道起始字符的时候也可以用来填补输入

In [10]: from itertools import co<TAB>
combinations                   compress
combinations_with_replacement  count
In [10]: import h<TAB>
hashlib             hmac                http         
heapq               html                husl

*:Tab只能知道起始字符的时候朝后填补,当你不知道起始字符的时候可以用星号

In [10]: *Warning?
BytesWarning                  RuntimeWarning
DeprecationWarning            SyntaxWarning
FutureWarning                 UnicodeWarning
ImportWarning                 UserWarning
PendingDeprecationWarning     Warning
ResourceWarning

星号代表任何字符,包括空字符,
所以如果你想找包含find的string相关的method,可以这样找:

In [10]: str.*find*?
str.find
str.rfind
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容