第1章 IPython入门

1.2 IPython

这个章节主要就是如何用ipython

help(len)
Help on built-in function len in module builtins:
len(obj, /)
    Return the number of items in a container.

len?

# ?可以查看方法文档

L = [1,2,3]
L.insert?
L?
def square(a):
    """
    return the square of a
    """
    return a**2
    
square?
square??
# ??可以查看源码
len??
L.<TAB>
  File "<ipython-input-10-b214fbf1057d>", line 1
    L.<TAB>
      ^
SyntaxError: invalid syntax


# tab键自动补全
L.clear
<function list.clear>
L.count
<function list.count>
L.__add__
# _下划线表示私有方法
<method-wrapper '__add__' of list object at 0x000001661243C548>
# 除了tab键自动补全,还有 * 通配符补全
*Warning?
"""
会列出所有匹配的方法
BytesWarning
DeprecationWarning
FutureWarning
ImportWarning
PendingDeprecationWarning
ResourceWarning
RuntimeWarning
SyntaxWarning
UnicodeWarning
UserWarning
Warning
"""
# 1.4 
# IPython魔法命令
# 1.4.1 %paste 粘贴
# 1.4.3 计算代码运行时间 %timeit
%timeit L =[n**2 for n in range(1000)]
276 µs ± 19.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
L=[]
for n in range (1000):
    L.append(n**2)
    
337 µs ± 31 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
1.6.2 魔法命令,ipython可以用 !和终端命令结合执行任何命令
例如
!ps
!pwd
1.8 错误和调试 
1.8.1 异常控制  %xmode
当出现异常的时候可以控制在“轨迹追溯”中找到错误原因,输出异常信息
def func1(a,b):
    return a/b
def func2(x):
    a = x
    b = x - 1
    return func1(a,b)# 执行 func2(1) 会出错
func2(1)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-6-7cb498ea7ed1> in <module>()
----> 1 func2(1)

<ipython-input-5-ee9d1acebbe5> in func2(x)
      2     a = x
      3     b = x - 1
----> 4     return func1(a,b)

<ipython-input-3-af923f9209db> in func1(a, b)
      1 def func1(a,b):
----> 2     return a/b

ZeroDivisionError: division by zero

%xmode 异常模式,可以改变输出信息
%xmode Plain # Plain 会使得输出信息变得紧凑,内容更少
Exception reporting mode: Plain
func2(1)
Traceback (most recent call last):

  File "<ipython-input-8-7cb498ea7ed1>", line 1, in <module>
    func2(1)

  File "<ipython-input-5-ee9d1acebbe5>", line 4, in func2
    return func1(a,b)

  File "<ipython-input-3-af923f9209db>", line 2, in func1
    return a/b

ZeroDivisionError: division by zero


还有Verbose 模式,可以增加额外信息,包括任何被调用的函数的参数
%xmode Verbose
Exception reporting mode: Verbose
func2(1)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-10-7cb498ea7ed1> in <module>()
----> 1 func2(1)
        global func2 = <function func2 at 0x000001807B3FF620>

<ipython-input-5-ee9d1acebbe5> in func2(x=1)
      2     a = x
      3     b = x - 1
----> 4     return func1(a,b)
        global func1 = <function func1 at 0x000001807B3FFBF8>
        a = 1
        b = 0

<ipython-input-3-af923f9209db> in func1(a=1, b=0)
      1 def func1(a,b):
----> 2     return a/b
        a = 1
        b = 0

ZeroDivisionError: division by zero

1.8.2 当阅读轨迹追溯不能解决问题的时候
%debug 魔法命令
%time
Wall time: 0 ns
%debug
1.9 代码分析和计时
# 魔法命令
%time  # 对单个语句执行时间计时
%prun  # 用分析器运行代码
%lprun  # 用逐行分析运行代码
%memit   # 测量单个语句的内存使用
%mprun   # 通过逐行的内存分析器运行代码1.9.1 代码段计时
%timeit 和 %time
​
​
​
​
​
​
​
​
​

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐