相关疑难解决方法(0)

我收到类型错误。我如何解决它?

我经常从我的 Python 代码中得到未捕获的异常(错误),这些异常被描述为TypeErrors. 经过大量的实验和研究,我收集了以下示例(以及细微的变化):

TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str …
Run Code Online (Sandbox Code Playgroud)

python debugging typeerror

11
推荐指数
2
解决办法
8049
查看次数

Python 3.6 中带元组的格式化字符串文字

随着str.format()我可以使用元组accesing参数:

>>> '{0}, {1}, {2}'.format('a', 'b', 'c')

'a, b, c'
Run Code Online (Sandbox Code Playgroud)

或者

>>> t = ('a', 'b', 'c')
>>> '{0}, {1}, {2}'.format(*t)

'a, b, c'
Run Code Online (Sandbox Code Playgroud)

但是对于以“f”(f 字符串)为前缀的新格式化字符串文字,我该如何使用元组?

f'{0}, {1}, {2}'.(*t)  # doesn't work
Run Code Online (Sandbox Code Playgroud)

python tuples string-formatting python-3.x python-3.6

5
推荐指数
1
解决办法
3750
查看次数