3

是否可以将 *args 传递给 string.format?我有以下功能:

@classmethod
def info(cls, component, msg, *args):
    """Log an info message"""
    cls.__log(cls.Level.INFO, component, msg, args)

@classmethod
def __log(cls, level, component, msg, *args):
    """Log a message at the requested level"""
    logging.getLogger("local").log(level, " - ".join([component, msg.format(args)]))

当我尝试使用 LogCapture 对其进行单元测试时,我得到以下信息:

def test_logWithArgs(self):
    Logger.level(Logger.Level.INFO)
    with LogCapture(level=Logger.Level.INFO) as lc:
        Logger.info("MyComponent", "{0}", "TestArg")
        lc.check(("local", "INFO", "MyComponent - TestArg"))


AssertionError: Sequence not as expected:

same:
()

first:
(('local', 'INFO', 'MyComponent - TestArg'),)

second:
(('local', 'INFO', "MyComponent - (('TestArg',),)"),)
4

2 回答 2

7

我想你想做的是

msg.format(*args)
于 2012-06-25T20:46:11.037 回答
3

是的。

>>> a=(1,2,3,4)
>>> "{0}{1}{2}{3}".format(*a)
'1234'
于 2012-06-25T20:49:02.307 回答