下面是我的代码片段。在运行python3.7中的代码时,我会得到错误SyntaxError: f-string: expressions nested too deeply
。我应该如何重构我的代码?
GRAPH_QL_FIELDS = """
cubeId
title
deleted
timeVariableFormat
statisticalProgram {
name
}
topics{
topicId
}
}
"""
query_topics = (
f'query cubesWithTopicLink($deleted: Boolean!, $topicId: String, $first: Int, $skip: Int) {{'
f'dataCubes(where:{AND:[{topics_some: {{topicId: $topicId}}}, {deleted: $deleted}]}, first: $first, skip: $skip) {{'
f'{GRAPH_QL_FIELDS}'
f'dataCubesConnection(where: {topics_some: {topicId: $topicId}})'
f'{{aggregate{{count}}}}'
f'}}'
)
发布于 2022-03-18 07:23:44
您可以使用多行字符串f"""This will work as expected with other nested strings '{3+5}'"""
。
发布于 2020-11-13 01:26:03
您不能像f-string
那样嵌套f'{f"my_var"}'
,所以应该通过删除嵌套的f-字符串并将它们划分为更多的f-字符串来重构代码。
我不认为格式化的字符串文本允许嵌套(嵌套,我把它理解为f‘{f“.}’)是仔细考虑可能的用例的结果,我更相信它只是为了使它们符合它们的规范而允许的。规范指出,它们位于方括号内,支持完整的Python表达式*。还声明格式化的字符串文本实际上只是在运行时计算的表达式(请参阅这里和这里)。因此,只允许格式化字符串文字作为另一个格式化字符串文本中的表达式是有意义的,禁止它将否定对Python表达式的完全支持。您无法找到文档中提到的用例(并且只在测试套件中找到测试用例),因为这可能是实现的一个很好的(副作用)效果,而不是它激励用例。
https://stackoverflow.com/questions/64817446
复制