在Python中将*args传递给string.format?

2024-03-29

是否可以将 *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',),)"),)

我想你想做的是

msg.format(*args)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在Python中将*args传递给string.format? 的相关文章

随机推荐