Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading