Skip to main content
 首页 » 编程设计

python之避免在 Sphinx 文档中扩展 sys.argv 关键字参数

2025年05月04日10arxive

我有这样一个类

import sys 
 
class ACommand(object): 
 
    @classmethod 
    def cmd(cls, argv=sys.argv): 
        " Executes the command. " 
        pass 

当用 Sphinx 记录它时,它会将 sys.argv 扩展为其当前值,生成类似于以下签名的内容

classmethod cmd(argv=['/path/to/sphinx-build', '-b', 'html', '-d', '_build/doctrees', '.', '_build/html']) 

我发现在文档中包含 sys.argv 比扩展更方便。我怎样才能做到这一点?

请您参考如下方法:

您可以创建一个代理对象,例如

class ListProxy(object): 
    def __init__(self, l): 
        self.list = l 
    def __getitem__(self, i): 
        return self.list[i] 

然后

@classmethod 
def cmd(cls, argv=ListProxy(sys.argv)): 
    " Executes the command. " 

或者,你可以做的更简单

@classmethod 
def cmd(cls, argv=None): 
    " Executes the command. " 
    if argv is None: 
        argv = sys.argv