[Python] How to apply multiple methods sequentially to class arguments

Discussão em 'Python' iniciado por Stack, Abril 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have a class which takes two arguments. I want to apply these two arguments to different methods. The output of each method should serve as the input for the next method. Additionally, I want this to work for all attributes without manually applying each method to every attribute. I want to use instance variables to store the results.

    Example:

    class Multiple_process:

    def __init__(self, param1, param2):
    self.param1 = param1
    self.param2 = param2

    # To store intermediate results
    self.param1_sq = None
    self.param2_sq = None
    self.param1_mul_10 = None
    self.param2_mul_10 = None

    def _square(self, value):
    sq_val = value ** 2
    return sq_val

    def _mul_10(self,sq_val):
    mul_10 = sq_val * 10
    return mul_10

    def applicaton(self):
    self.param1_sq = self._square(self.param1)
    self.param2_sq = self._square(self.param2)
    self.param1_mul_10 = self._mul_10(self.param1_sq)
    self.param2_mul_10 = self._mul_10(self.param2_sq)
    return self.param1_sq, self.param2_sq, self.param1_mul_10, self.param2_mul_10

    test = Multiple_process(5, 4)
    print(test.applicaton())


    The output is (25, 16, 250, 160), which is as expected. But suppose one is working on a complex project, then this way of creating multiple instance variables seems quite tiresome and illogical to store the results of each method applied to each argument. We may be interested in the final output of the last method in a complex project, but how do we handle the intermediate results?

    My requirements:

    1. Class takes arguments.
    2. They are passed into different methods.
    3. Output of one method is input to the later one
    4. Ultimately produces final output for both arguments, which are to be used for a different purpose.

    How can we simplify, or streamline, this whole process?

    Continue reading...

Compartilhe esta Página