Is it impossible to transfer functions to Python3 class?

Asked 2 years ago, Updated 2 years ago, 68 views

class PrintFuncNum:
    def __init__(self, func):
        self.func = func

    def check(self,  test):
        answer = self.func(test)

test1 = PrintFuncNum(func) 
# You can put test*3 in the answer in the check part
# Or I would like to add +3 or create a class by the user's designation.

As above, I would like to create an instance by making a func factor from the code I want.
Is there a way?

class function python

2022-09-20 17:45

1 Answers

Please refer to the code below.

class PrintFuncNum:
    def __init__(self, func):
        self.func = func
        self.answer = 10

    def check(self,  test):
        self.answer = self.func(self.answer, test)
        print(self.answer)

def add(a,b):
    return a+b

def multiply(a,b):
    return a*b


test1 = PrintFuncNum(add)
test1.check(3)

test2 = PrintFuncNum(multiply)
test2.check(3)


2022-09-20 17:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.