可以像这样混合普通字符串和新的 f-string 吗:
import timeit
format = """
def format(name, age):
return (
f'He said his name is '
f'{name} and he is '
f'{age} years old.'
)
""", """
def format(name, age):
return (
'He said his name is '
f'{name} and he is '
f'{age} years old.'
)
"""
test = """
def test():
for name in ('Fred', 'Barney', 'Gary', 'Rock', 'Perry', 'Jackie'):
for age in range (20, 200):
format(name, age)
"""
for fmt in format:
print(timeit.timeit('test()', fmt + test, number=10000))
[out]:
3.4188902939995387
3.3931472289996236
这可以使用吗,或者混合普通弦和 f 弦不被认为是最佳做法?
呼啦一阵风
相关分类