我在用 yield 处理列表中的每个元素。但是,如果元组只有一个字符串元素, 产量 返回字符串的字符,而不是整个字符串:
yield
产量
self.commands = ("command1") ... for command in self.commands: yield command # returns 'c' not 'command1'
我怎样才能解决这个问题?
谢谢
只有一个元素的元组应该是 written with a trailing comma
self.commands = ("command1",)
self.commands = ["command1"]
你从来没有告诉循环你有一个列表,所以它把字符串当作序列。