0

From the below function, I am able to print the information of crew_filter and node. But I need help on returning those values to the target script. And also how can I access them in the target script?

def getValuesForNode(self):
   for crew_filter, node in self.getNodes():
       print crew_filter, node

Note: As crew_filter and node will have multiple values, I need to store them in a tuple or dictionary.

zondo
  • 19,901
  • 8
  • 44
  • 83
srisriv
  • 107
  • 1
  • 2
  • 10

1 Answers1

1

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?

Shi XiuFeng
  • 715
  • 5
  • 13