1, __str__和__repr__的区别
In [1]: class DoubleRep(object): ...: def __str__(self): ...: return "Hi,I‘m a __str__" ...: def __repr__(self): ...: return "Hi,I‘m a __repr" ...: ...: In [2]: dr = DoubleRep() In [3]: print dr ------> print(dr) Hi,I‘m a __str__ In [4]: dr Out[5]: Hi,I‘m a __repr
使用print输出对象dr时,__str__方法被调用
使用正式字符串表达式dr时,__repr__方法被调用
2, Ipython shell 提示符:In和Out
In [5]: type(In) Out[5]: <class ‘IPython.iplib.InputList‘> In [6]: type(Out) Out[6]: <type ‘dict‘> In [7]: print In ------> print(In) [‘\n‘, u‘class DoubleRep(object):\n def __str__(self):\n return "Hi,I\‘m a __str__"\n def __repr__(self):\n return "Hi,I\‘m a __repr"\n \n\n‘, u‘dr = DoubleRep()\n‘, u‘print(dr)\n‘, u‘dr\n‘, u‘type(In)\n‘, u‘type(Out)\n‘, u‘print(In)\n‘] In [8]: print Out ------> print(Out) {4: Hi,I‘m a __repr, 5: <class ‘IPython.iplib.InputList‘>, 6: <type ‘dict‘>}
In就是一个列表
Out就是一个字典
3, Tab自动完成
普通python shell需要开启Tab补齐功能
import rlcompleter, readline readline.parse_and_bind(‘tab: complete‘)
Ipython中Tab不仅可以补齐命令,还能在载入模块时补齐模块名称
4, Ipython配置文件
安装完这个版本的Ipthon后在home目录下会自动创建.ipython目录
其中ipy_user_conf.py是个配置文件
5, 魔力函数
以%开始,参数中不包含括号或者引号,例如,%cd mydir
lsmagic 列出所有魔力函数
%<Tab> 同样也是列出所有魔力函数
输入magic可以打开包含所有魔力函数的帮助文档
如何你已经知道某个魔力函数,可以这样来查看帮助 %page ?
%quickref 是一个快速参考文档,包含了对%magic函数的迷你总结
6, alias 即创建一个别名,能够在ipython中运行bash shell命令
In [1]: alias nss netstat -lptn In [2]: nss Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2022/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1543/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1546/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1965/master tcp6 0 0 :::22 :::* LISTEN 1543/sshd tcp6 0 0 ::1:631 :::* LISTEN 1546/cupsd tcp6 0 0 ::1:25 :::* LISTEN 1965/master In [3]: nss | grep 80 #别名后面可以查找某个关键字
In [5]: alias achoo echo "|%l|" #%l就代表了字符串,输出""内所有字符 In [6]: achoo || In [7]: achoo these are args |these are args|
In [8]: alias achoo echo "%l" In [9]: achoo these are args these are args
In [16]: alias achoo echo first:"|%s|", second:"|%s|" In [17]: achoo foo bar first:|foo|, second:|bar|
即代表achoo为echo输出后面这些字符,%s代表字符串,有先后顺序。
数量上少了会报错,多了会继续输出在尾部,如:
In [18]: achoo foo bar bam first:|foo|, second:|bar| bam
保存别名,下次进入ipthon还能继续使用
In [20]: store achoo Alias stored: achoo (2, ‘echo first:"|%s|", second:"|%s|"‘)
Python.Unix和Linux系统管理指--读书笔记--第二章
原文:http://142623.blog.51cto.com/132623/1892233