Python解释器和IPython

简介

今天给你们介绍一下Python的一个功能很是强大的解释器IPython。虽然Python自己自带解释器,可是相对而言IPython的功能更加的强大。html

Python解释器

Python是自带解释器的,咱们在命令行输入python便可进入python的解释器环境:python

$> python
Python 2.7.15 (default, Oct  2 2018, 11:47:18)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> site = "www.flydean.com"
>>> site
'www.flydean.com'
>>>

python解释器的提示符是>>>git

python提供了一个很是有用的命令help,咱们可使用help来查看要使用的命令。shell

>>> help
Type help() for interactive help, or help(object) for help about object.

在Python3中,还提供了tab的补全功能:express

>>> site
'www.flydean.com'
>>> site.
site.capitalize(    site.expandtabs(    site.isalpha(       site.isprintable(   site.lower(         site.rindex(        site.splitlines(    site.upper(
site.casefold(      site.find(          site.isdecimal(     site.isspace(       site.lstrip(        site.rjust(         site.startswith(    site.zfill(
site.center(        site.format(        site.isdigit(       site.istitle(       site.maketrans(     site.rpartition(    site.strip(
site.count(         site.format_map(    site.isidentifier(  site.isupper(       site.partition(     site.rsplit(        site.swapcase(
site.encode(        site.index(         site.islower(       site.join(          site.replace(       site.rstrip(        site.title(
site.endswith(      site.isalnum(       site.isnumeric(     site.ljust(         site.rfind(         site.split(         site.translate(

使用起来很是的方便。api

和Python自带的解释器以外,还有一个更增强大的解释器叫作IPython。咱们一块儿来看看。ide

IPython

IPython是一个很是强大的解释器,一般它是和jupyter notebook一块儿使用的。在IPython3.X中,IPython和Jupyter是做为一个总体一块儿发布的。可是在IPython4.X以后,Jupyter已经做为一个单独的项目,从IPython中分离出来了。函数

使用IPython很简单,输入IPython命令便可:oop

$> ipython
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: site= "www.flydean.com"

In [2]: site
Out[2]: 'www.flydean.com'

IPython的提示符是In [1]:ui

基本上Python自带的命令在IPython中都是可使用的。

IPython提供了4个很是有用的命令:

command description
? Introduction and overview of IPython’s features.
%quickref Quick reference.
help Python’s own help system.
object? Details about ‘object’, use ‘object??’ for extra details.

魔法函数

IPython中有两种魔法函数,一种是Line magics,一种是Cell magics

Line magics 接收本行的输入做为函数的输入,是以%开头的。而Cell magics能够接收多行的数据,直到你输入空白回车为止。是以%%开头的。

好比咱们想要看一个timeit的魔法函数的用法,可使用Object?来表示:

$> In [4]: %timeit?
Docstring:
Time execution of a Python statement or expression

Usage, in line mode:
  %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
  %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
  code
  code...

timeit用来统计程序的执行时间,咱们分别看下Line magics和Cell magics的使用:

In [4]: %timeit?

In [5]: %timeit range(1000)
199 ns ± 3.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [6]: %%timeit range(1000)
   ...: range(1000)
   ...:
208 ns ± 12.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
事实上,若是只是LIne magics的话,咱们能够省略前面的%,可是对于Cell magics来讲,是不能省略的。
In [7]: timeit range(1000)

200 ns ± 4.03 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

常见的魔法函数有下面几种:

运行和编辑

使用%run 能够方便的运行外部的python脚本。

In [8]: run?
Docstring:
Run the named file inside IPython as a program.

Usage::

  %run [-n -i -e -G]
       [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
       ( -m mod | file ) [args]

run有几个很是有用的参数,好比-t 能够用来统计程序的时间。-d能够进行调试环境,-p能够进行profiler分析。

使用%edit 能够编辑多行代码,在退出以后,IPython将会执行他们。

若是不想当即执行的话,能够加上-x参数。

Debug

可使用%debug 或者 %pdb 来进入IPython的调试环境:

In [11]: debug
> /Users/flydean/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/IPython/core/compilerop.py(99)ast_parse()
     97         Arguments are exactly the same as ast.parse (in the standard library),
     98         and are passed to the built-in compile function."""
---> 99         return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
    100
    101     def reset_compiler_flags(self):

ipdb>
In [12]: pdb
Automatic pdb calling has been turned ON

In [13]: pdb
Automatic pdb calling has been turned OFF

或者可使用 %run -d theprogram.py 来调试一个外部程序。

History

IPython能够存储你的输入数据和程序的输出数据,IPython的一个很是重要的功能就是能够获取到历史的数据。

在交互环境中,一个简单的遍历历史输入命令的方式就是使用up- 和 down- 箭头。

更强大的是,IPython将全部的输入和输出都保存在In 和 Out这两个变量中,好比In[4]。

In [1]: site = "www.flydean.com"

In [2]: site
Out[2]: 'www.flydean.com'

In [3]: In
Out[3]: ['', 'site = "www.flydean.com"', 'site', 'In']

可使用 _ih[n]来访问特定的input:

In [4]: _ih[2]
Out[4]: 'site'

_i, _ii, _iii 能够分别表示前一个,前前一个和前前前一个输入。

除此以外,全局变量 _i<n> 也能够用来访问输入,也就是说:

_i<n> == _ih[<n>] == In[<n>]
_i14 == _ih[14] == In[14]

一样的,对于输出来讲也存在着三种访问方式:

_<n> == _oh[<n>] == Out[<n>]
_12 == Out[12] == _oh[12]

最后的三个输出也能够经过 _, _____来获取。

还可使用%history来列出以前的历史数据进行选择。

history能够和 %edit, %rerun, %recall, %macro, %save%pastebin 配和使用:

经过传入数字,能够选择历史的输入行号。

%pastebin 3 18-20

上面的例子会选择第3行和第18-20行输入。

运行系统命令

使用!能够直接运行系统命令:

In [27]: !pwd
/Users/flydean/Downloads

还能够用变量接收运行的结果,好比 : files = !ls

本文做者:flydean程序那些事

本文连接:http://www.flydean.com/python-ipython/

本文来源:flydean的博客

欢迎关注个人公众号:「程序那些事」最通俗的解读,最深入的干货,最简洁的教程,众多你不知道的小技巧等你来发现!

相关文章
相关标签/搜索