在我们的python编程中,有时候需要涉及到很多命令的方法,这些方法可能有上百个上千个,一般谁也记不住,可以使用下面的方法,让python自动帮助我们补全这些命令,这样编程时就会方便而且快速完成。
方法如下:
import rlcompleter
import readline
import atexit
import os
# http://stackoverflow.com/questions/7116038/python-tab-completion-mac-osx-10-7-lion
if'libedit'in readline.__doc__:
readline.parse_and_bind('bind ^I rl_complete')
else:
readline.parse_and_bind('tab: complete')
histfile = os.path.join(os.environ['HOME'],'.pyhist')
try:
readline.read_history_file(histfile)
exceptIOError:
pass
atexit.register(readline.write_history_file, histfile)
del readline, rlcompleter, histfile, os
将上方的代码,编辑成一个*.py的文件,在使用之前,可以使用import命令导入这个文件,就可以实现补全代码的功能了。