为什么 f'{None}_some_string' 不抛出错误?
Why f'{None}_some_string' not throw an error?
我想知道为什么 f'{None}_some_string'
不抛出错误?
这里有一些重现问题的例子:
s1 = None # <class 'NoneType'>
s2 = 'real_string' # <class 'str'>
s1 + s2 # TypeError: unsupported operand type(s) for +: 'NoneType' and 'str
f'{s1}_bla'
'None_bla'
f'{s2}_bla'
'real_string_bla'
当 s
为 None
时,是否可以让像 f'{s}_some_string'
这样的表达式抛出错误?
默认情况下,f-string 应用 str()
(字符串)函数。
f'{None}_some_string'
相当于...
f'{str(None)}_some_string}'
我想知道为什么 f'{None}_some_string'
不抛出错误?
这里有一些重现问题的例子:
s1 = None # <class 'NoneType'>
s2 = 'real_string' # <class 'str'>
s1 + s2 # TypeError: unsupported operand type(s) for +: 'NoneType' and 'str
f'{s1}_bla'
'None_bla'
f'{s2}_bla'
'real_string_bla'
当 s
为 None
时,是否可以让像 f'{s}_some_string'
这样的表达式抛出错误?
默认情况下,f-string 应用 str()
(字符串)函数。
f'{None}_some_string'
相当于...
f'{str(None)}_some_string}'