IndexError: tuple index out of range when parsing method arguments

依然范特西╮ 提交于 2019-12-07 02:35:03

问题


I've already checked this question, but couldn't find an answer there. Here is a simple example that demonstrates my use case:

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")

And the output:

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: I've already refused using such a method (that wraps str.format()), as it seems to be excess. But still it puzzles me, why wouldn't this work as expected?


回答1:


you have to use * to unpack the tuple into actual arguments for format:

print(message.format(*arguments))

otherwise, arguments is seen as the sole argument of format (and it works for the first {} occurrence, by converting your tuple to string, but chokes when it encounters the second occurrence of {})




回答2:


You need to pass arguments not tuple. This is done by using '*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")  



回答3:


Try this

print(message.format(*arguments))

format does not expect a tuple



来源:https://stackoverflow.com/questions/41631992/indexerror-tuple-index-out-of-range-when-parsing-method-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!