Home > Blockchain >  Simple function-composition class does not work (missing 2 required positional arguments)
Simple function-composition class does not work (missing 2 required positional arguments)

Time:05-04

I am digging into math currently and tried to build a simple function composition maker

https://en.wikipedia.org/wiki/Function_composition

I want to tell the program:

Look at two tuples and compare them:

tuple_1 = (2,4)
tuple_2 = (5,2)

Do tuple_2[1] and tuple_1[0] have the same value? (both = 2)

If yes:

Return tuple(tuple_2[0]) and tuple_1[1]

5 4

My function works:

list_of_tuples_1 = [
    (2,4),
    (2,5)    
    ]

list_of_tuples_2 = [
    (1,2),
    (5,2)
    ]

def give_function_composition(list_1, list_2):
    for tuples_2 in list_2:
        for tuples_1 in list_1:
            if tuples_2[1] == tuples_1[0]:
                return(str(tuples_2[0])   " "   str(tuples_1[1]))

                
print(give_function_composition(list_of_tuples_1, list_of_tuples_2))

My class does not:

class Function_Composition():
    def __init__(self, list_1, list_2):
        self.list_1 = list_1
        self.list_2 = list_2
        
    def give_function_composition(self, list_1, list_2):
        for tuples_2 in list_2:
            for tuples_1 in list_1:
                if tuples_2[1] == tuples_1[0]:
                    return(str(tuples_2[0])   " "   str(tuples_1[1]))        

                
my_set_1 = Function_Composition([(3,5),(1,5),(5,4)],
                                 [(5,7),(3,6),(1,8)])

print(my_set_1.give_function_composition())
TypeError: give_function_composition() missing 2 required positional arguments: 'list_1' and 'list_2'

What is the issue here? I gave him two lists, didn't I?

CodePudding user response:

You should refer to the instance data passed into the constructor, rather than passing in the same list twice:

class Function_Composition():
    def __init__(self, list_1, list_2):
        self.list_of_tuples_01 = list_1
        self.list_of_tuples_02 = list_2
        
    def give_function_composition(self):
        for tuples_2 in self.list_of_tuples_02:
            for tuples_1 in self.list_of_tuples_01:
                if tuples_2[1] == tuples_1[0]:
                    return(str(tuples_2[0])   " "   str(tuples_1[1]))
        return "No relation found."

                
my_set_1 = Function_Composition([(3,5),(1,5),(5,4)],
                                 [(5,7),(3,6),(1,8)])

print(my_set_1.give_function_composition())

This outputs:

No relation found.

  • Related