关于魔术命令

作者: 庵下桃花仙 | 来源:发表于2018-12-04 22:14 被阅读14次

魔术命令前缀符号为“%”

In [1]: import numpy as np

In [2]: a = np.random.randn(100, 100)

In [3]: %timeit np.dot(a, a)
82.8 µs ± 636 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [4]: %debug?
Docstring:
::

  %debug [--breakpoint FILE:LINE] [statement [statement ...]]

Activate the interactive debugger.

This magic command support two ways of activating debugger.
One is to activate debugger before executing code.  This way, you
can set a break point, to step through the code from the point.
You can use this mode by giving statements to execute and optionally
a breakpoint.

The other one is to activate debugger in post-mortem mode.  You can
activate this mode simply running %debug without any argument.
If an exception has just occurred, this lets you inspect its stack
frames interactively.  Note that this will always work only on the last
traceback that occurred, so you must call this quickly after an
exception that you wish to inspect has fired, because if another one
occurs, it clobbers the previous one.

If you want IPython to automatically do this on every exception, see
the %pdb magic for more details.

positional arguments:
  statement             Code to run in debugger. You can omit this in cell
                        magic mode.

optional arguments:
  --breakpoint <FILE:LINE>, -b <FILE:LINE>
                        Set break point at LINE in FILE.
File:      c:\users\dell\appdata\local\programs\python\python37-32\lib\site-packages\ipython\core\magics\execution.py

魔术函数也可以不加“%”使用。这种特性称之为自动魔术,通过%automagic进行启用/禁用。

In [8]: %pwd
Out[8]: 'C:\\Users\\Dell'

In [9]: foo = %pwd

In [10]: foo
Out[10]: 'C:\\Users\\Dell'

一些魔术函数像Python函数一样,其输出可以赋值给一个变量。

相关文章

网友评论

    本文标题:关于魔术命令

    本文链接:https://www.haomeiwen.com/subject/sacbcqtx.html