IndexError: tuple index out of range when parsing method arguments
IndexError: tuple index out of range when parsing method arguments
我已经检查了 this 个问题,但找不到答案。这是一个演示我的用例的简单示例:
def log(*args):
message = str(args[0])
arguments = tuple(args[1:])
# message itself
print(message)
# arguments for str.format()0
print(arguments)
# shows that arguments have correct indexes
for index, value in enumerate(arguments):
print("{}: {}".format(index, value))
# and amount of placeholders == amount of arguments
print("Amount of placeholders: {}, Amount of variables: {}".format(message.count('{}'), len(arguments)))
# But this still fails! Why?
print(message.format(arguments))
log("First: {}, Second: {}, Third: {}, Fourth: {}", "asdasd", "ddsdd", "12312333", "fdfdf")
并且输出:
First: {}, Second: {}, Third: {}, Fourth: {}
('asdasd', 'ddsdd', '12312333', 'fdfdf')
0: asdasd
1: ddsdd
2: 12312333
3: fdfdf
Amount of placeholders: 4, Amount of variables: 4
Traceback (most recent call last):
File "C:/Users/sbt-anikeev-ae/IdeaProjects/test-this-thing-on-python/test-this-thing.py", line 12, in <module>
log("First: {}, Second: {}, Third: {}, Fourth: {}", "asdasd", "ddsdd", "12312333", "fdfdf")
File "C:/Users/sbt-anikeev-ae/IdeaProjects/test-this-thing-on-python/test-this-thing.py", line 10, in log
print(message.format(arguments))
IndexError: tuple index out of range
P.S: 我已经拒绝使用这种方法(包装 str.format()
),因为它似乎是多余的。但是还是很疑惑,为什么没有达到预期的效果呢?
您必须使用 *
将元组解压为 format
:
的实际参数
print(message.format(*arguments))
否则,arguments
被视为 format 的唯一参数(它适用于第一个 {}
出现,通过将你的元组转换为字符串,但是当它遇到第二次出现时窒息{}
)
您需要传递参数而不是元组。这是通过使用“*arguments”来完成的。
Expanding tuples into arguments
def log(*args):
message = str(args[0])
arguments = tuple(args[1:])
# message itself
print(message)
# arguments for str.format()0
print(arguments)
# shows that arguments have correct indexes
for index, value in enumerate(arguments):
print("{}: {}".format(index, value))
# and amount of placeholders == amount of arguments
print("Amount of placeholders: {}, Amount of variables: {}".format(message.count('{}'), len(arguments)))
# But this still fails! Why?
print(type(arguments))
print(message.format(*arguments))
log("First: {}, Second: {}, Third: {}, Fourth: {}", "asdasd", "ddsdd", "12312333", "fdfdf")
试试这个
print(message.format(*arguments))
format
不期望元组
我已经检查了 this 个问题,但找不到答案。这是一个演示我的用例的简单示例:
def log(*args):
message = str(args[0])
arguments = tuple(args[1:])
# message itself
print(message)
# arguments for str.format()0
print(arguments)
# shows that arguments have correct indexes
for index, value in enumerate(arguments):
print("{}: {}".format(index, value))
# and amount of placeholders == amount of arguments
print("Amount of placeholders: {}, Amount of variables: {}".format(message.count('{}'), len(arguments)))
# But this still fails! Why?
print(message.format(arguments))
log("First: {}, Second: {}, Third: {}, Fourth: {}", "asdasd", "ddsdd", "12312333", "fdfdf")
并且输出:
First: {}, Second: {}, Third: {}, Fourth: {}
('asdasd', 'ddsdd', '12312333', 'fdfdf')
0: asdasd
1: ddsdd
2: 12312333
3: fdfdf
Amount of placeholders: 4, Amount of variables: 4
Traceback (most recent call last):
File "C:/Users/sbt-anikeev-ae/IdeaProjects/test-this-thing-on-python/test-this-thing.py", line 12, in <module>
log("First: {}, Second: {}, Third: {}, Fourth: {}", "asdasd", "ddsdd", "12312333", "fdfdf")
File "C:/Users/sbt-anikeev-ae/IdeaProjects/test-this-thing-on-python/test-this-thing.py", line 10, in log
print(message.format(arguments))
IndexError: tuple index out of range
P.S: 我已经拒绝使用这种方法(包装 str.format()
),因为它似乎是多余的。但是还是很疑惑,为什么没有达到预期的效果呢?
您必须使用 *
将元组解压为 format
:
print(message.format(*arguments))
否则,arguments
被视为 format 的唯一参数(它适用于第一个 {}
出现,通过将你的元组转换为字符串,但是当它遇到第二次出现时窒息{}
)
您需要传递参数而不是元组。这是通过使用“*arguments”来完成的。 Expanding tuples into arguments
def log(*args):
message = str(args[0])
arguments = tuple(args[1:])
# message itself
print(message)
# arguments for str.format()0
print(arguments)
# shows that arguments have correct indexes
for index, value in enumerate(arguments):
print("{}: {}".format(index, value))
# and amount of placeholders == amount of arguments
print("Amount of placeholders: {}, Amount of variables: {}".format(message.count('{}'), len(arguments)))
# But this still fails! Why?
print(type(arguments))
print(message.format(*arguments))
log("First: {}, Second: {}, Third: {}, Fourth: {}", "asdasd", "ddsdd", "12312333", "fdfdf")
试试这个
print(message.format(*arguments))
format
不期望元组