2

我有一堂像这样的课

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文档中的内容比其扩展更方便。我怎样才能做到这一点?

4

1 回答 1

0

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

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
于 2012-10-02T10:22:03.373 回答