python完成,文件vs obj

2024-09-20 22:19:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近在python interperter中发现了auto completetion:https://docs.python.org/2.7/tutorial/interactive.html。 这对于加速我在交互式解释器中所做的测试是非常好的。有两件事是可以完成的,它们都是有用的。你知道吗

如果我简单地将C+f: complete放在我的.inputrc中(或者使用没有rlcompleter的readline),当我按下Ctl+f时,我会在启动解释器的目录中完成文件。当我加载模块readlinerlcompleter并将readline.parse_and_bind('C-n: complete')添加到.pystartup文件时,它将Ctl+n和Ctl+f转换为自动完成的python对象。你知道吗

我想两者都做,但不确定如何防止rlcompleter重写标准完成。有没有办法启动两个readline实例,一个使用,一个不使用rlcompleter?你知道吗

这是我的.pystartup文件

import atexit
import os
import readline
import rlcompleter #removing this does file completion.

readline.parse_and_bind('C-n: complete')

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath

Tags: and文件importreadlineparseossave解释器
1条回答
网友
1楼 · 发布于 2024-09-20 22:19:31

工作原理:

导入rlcompleter时,它将rlcompleter.Completer().complete安装为readline的完成符:readline.set_completer(Completer().complete)。你知道吗

如果没有rlcompletercompleterNone,那么底层GNU readline lib默认使用rl_filename_completion_function。你知道吗

绑定键和完成逻辑是由GNU readline lib实现的,因此在Python中没有任何关于start up two instances of readline的操作。。。你知道吗


我没有找到在Python中调用default rl_filename_completion_function的方法(在C扩展中是可能的),所以我想您必须在Python中复制rl_filename_completion_function的逻辑。你知道吗

这意味着您应该继承Completer并构建您的自定义complete。但是您仍然不能将这两个逻辑拆分为C-nC-f:(

相关问题 更多 >

    热门问题