小编dar*_*arw的帖子

Python 'string' % [1, 2, 3] 不会引发 TypeError

是否记录了确切的行为str.__mod__

这两行代码按预期工作:

>>> 'My number is: %s.' % 123
'My number is: 123.'
>>> 'My list is: %s.' % [1, 2, 3]
'My list is: [1, 2, 3].'
Run Code Online (Sandbox Code Playgroud)

此行也按预期运行:

>>> 'Not a format string' % 123
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)

但是这条线是什么以及为什么它不会引发任何错误?

>>> 'Not a format string' % [1, 2, 3]
'Not a format string'
Run Code Online (Sandbox Code Playgroud)

聚苯乙烯

>>> print(sys.version)
3.3.2 (default, Aug 15 2013, …
Run Code Online (Sandbox Code Playgroud)

python list modulo string-interpolation python-3.2

14
推荐指数
2
解决办法
314
查看次数

标签 统计

list ×1

modulo ×1

python ×1

python-3.2 ×1

string-interpolation ×1