简体   繁体   中英

Call a method on a class instance (function in class) that calls other functions within that class

I am working in a Class in Python and it has 3 functions in it. Function 1 and Function 2 are acting on their own but function 3 should print what is returned from lets say func_1.
I passed the function names of func_1 into func_3 but I just cant get the right result. When I create an instance of the class named "test", I cant call test.func_3 and I tried everything I know or was able to find on the internet.

Tried to call it without passing func_1 into func_3: "NameError: name 'func_1' is not defined"

Passed func_1 into func_3 and called it without argument: "TypeError: func_3() missing 1 required positional argument: 'func_1'"

Passed func_1 into func_3 and called it with argument: "NameError: name 'func_1' is not defined"

This is working code (without a class, just some functions:

def func_1():
    print("Print func_1 - Print_string")
    return "ReturnFunc1 - Return_string | "

def func_2():
    print("Print func_2 - Print_string")
    return "ReturnFunc2 - Return_string | "

def func_3():
    print("Anything below this line is printed because of calling func_3")
    print("============================================================")
    print("Print func_3 - Print_string")
    print(func_1(), func_2())

func_1()
func_2()
func_3()

==================================================================

This is with a class, not working anyhow:

class TestingClass():

    def func_1(self):
        print("Print func_1")
        return "ReturnFunc1"

    def func_2(self):
        print("Print func_2")
        return "ReturnFunc2"

    def func_3(self,func_1):
        print("Anything below this line is printed because of calling func_3")
        print("============================================================")
        print("Print func_3")
        print(func_1)

test = TestingClass()
test.func_3(func_1)

I would like to achieve the same results as when I code it without Class.

Either like this:

class TestingClass():

    def func_1(self):
        print("Print func_1")
        return "ReturnFunc1"

    def func_2(self):
        print("Print func_2")
        return "ReturnFunc2"

    def func_3(self,func_1):
        print("Anything below this line is printed because of calling func_3")
        print("============================================================")
        print("Print func_3")
        print(func_1())

test = TestingClass()
test.func_3(test.func_1)

Or like this:

class TestingClass():

    def func_1(self):
        print("Print func_1")
        return "ReturnFunc1"

    def func_2(self):
        print("Print func_2")
        return "ReturnFunc2"

    def func_3(self):
        print("Anything below this line is printed because of calling func_3")
        print("============================================================")
        print("Print func_3")
        print(self.func_1())

test = TestingClass()
test.func_3()

In the first snippet, notice the change from print(func_1) to print(func_1()) . More importantly, also notice the change from test.func_3(func_1) to test.func_3(test.func_1) .

The second snippet is different in the sense that you are not passing the function to func_3 anymore, and instead just invoking the class instance's func_1 method directly.

Playing around with your case, you can use global if you want to call func_1() outside the class and treat it as normal function instead of method.

class TestingClass():

    global func_1

    def func_1():
        print("Print func_1")
        return "ReturnFunc1"

    def func_2(self):
        print("Print func_2")
        return "ReturnFunc2"

    def func_3(self, func_1):
        print("Anything below this line is printed because of calling func_3")
        print("============================================================")
        print("Print func_3")
        print(func_1)


test = TestingClass()
test.func_3(func_1())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM