If the class Test
is written in the script test.py
.
test.py
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
Then you can access your value by the generator defined above in another script, for example foo.py
:
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))
Output
1 - 2
3 - 4
5 - 6
You can replace the var test_data
with your data source, either tuple or dict, if you wanna iterate a dict you have to do it like following:
class Test:
def get_values_for_node(self):
test_data = {
'id':1,
'name': 'tom',
'age': 23
}
for crew_filter, node in test_data.items():
yield crew_filter, node
Output
id - 1
name - tom
age - 23
If you are unfamiliar with the usage of the yield
or the concept of generator
, you can view this page:
What does the "yield" keyword do in Python?