如何遍历由执行特定功能的类组成的管道,并将结果传递给下一个类?

时间:2019-07-16 09:34:07

标签: python python-3.x pipeline

想象一下,我们有一个代表管道的类。该管道是代表不同操作的对象列表。该管道的第一个元素从Pipe类获取其输入参数。然后,每个对象的输出将成为下一个对象输入参数。

class Pipe():

    def build_pipeline(self):
        Stage1 = Object_X()
        Stage2 = Object_Y() 
        self.pipeline = [Stage1, Stage2]

    def run():
        for p in pipeline:
            # execute p's respective run()
            # pass this to the next element in pipeline      

class Object_X():
   # all kinds of definition

   def run()
       # do something

class Object_Y():
   # all kinds of definition

   def run()
       # do something

一个人如何应对这一挑战?我可以使用任何模式吗?

谢谢

1 个答案:

答案 0 :(得分:0)

在最简单的情况下,您可以声明并重新分配输入参数/参数:

class Pipe():
    def build_pipeline(self):
        Stage1 = Object_X()
        Stage2 = Object_Y() 
        self.pipeline = [Stage1, Stage2]

    def run():
        input_arg = self.input_arg   # initial input from Pipe class
        for p in pipeline:
            input_arg = p.run(input_arg)