class Test:
def get_values_for_node(self):
test_data = (
(1, 2),
(3, 4),
(5, 6)
)
for crew_filter, node in test_data:
yield crew_filter, node
然后,您可以通过上面在另一个脚本中定义的生成器访问您的值,例如foo.py:
福比
from test import Test
if __name__ == '__main__':
test = Test()
for crew_filter, node in test.get_values_for_node():
print('{0} - {1}'.format(crew_filter, node))
如果类
Test
是在脚本test.py
中编写的test.py
然后,您可以通过上面在另一个脚本中定义的生成器访问您的值,例如
foo.py
:福比
输出
您可以用数据源(元组或dict)替换var
test_data
,如果您想迭代dict,您必须按照以下方式执行:输出
如果您不熟悉
yield
的用法或generator
的概念,可以查看此页面:What does the "yield" keyword do in Python?
相关问题 更多 >
编程相关推荐