我正在尝试使用带有映射的 python 分层数据结构,并且值将是一个元组。在某些情况下,元组的长度为 1。只要元组的长度为 1,Python 就会智能地展平结构。观察下面的示例,该示例可以在 python 解释器中运行。在“another_scenario”中,我预计长度为 1,但它在下面钻了一层并得到了基础步骤。这完全搞砸了我的测试,因为我依赖于它是一个带有命令、function_list、函数列表的元组。
问题 - 为什么会发生这种情况?我如何要求python不要展平它?
import os
def run():
my_scenario = {
"scenario_name" :
( # Each scenario is a List of (command, function_list, function_list)
# function_list = one function OR tuple of functions
(
"command1",
(
os.path,
os.path.exists
),
None
),
(
"command2",
(
os.path,
os.path.exists
),
None
)
)
}
another_scenario = {
"scenario_name" :
(
(
"command1",
(
os.path,
os.path.exists
),
None
)
)
}
for name in my_scenario:
print "Full Scenario is %s" % str(my_scenario[name])
print "Length should be 2 -> %s" % len(my_scenario[name])
for name in another_scenario:
print "Full Scenario is %s" % str(another_scenario[name])
print "Length should be 1 -> %s" % len(another_scenario[name]) #Prints 3 as it drills one level down
if __name__ == "__main__":
run()