调用类实例(类中的函数)上的方法,该方法调用该类中的其他函数

时间:2019-08-29 14:59:26

标签: python python-3.x class

我在Python中的类中工作,它具有3个函数。 功能1和功能2各自发挥作用,但是功能3应该打印出从func_1返回的内容。
我将func_1的函数名称传递给func_3,但是我无法获得正确的结果。创建名为“ test”的类的实例时,无法调用test.func_3,并且尝试了所有我知道的或能够在Internet上找到的东西。

试图在不将func_1传递到func_3的情况下调用它: “ NameError:名称'func_1'未定义”

将func_1传递给func_3,并且不带参数调用它: “ TypeError:func_3()缺少1个必需的位置参数:'func_1'”

将func_1传递给func_3并使用参数调用它: “ NameError:名称'func_1'未定义”

这是有效的代码(没有类,只有一些功能:

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()

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

这是一个类,无论如何都不起作用:

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)

我希望获得与不使用Class编码时相同的结果。

2 个答案:

答案 0 :(得分:1)

像这样:

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)

或者这样:

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()

在第一个代码段中,请注意从print(func_1)更改为print(func_1())。更重要的是,还要注意从test.func_3(func_1)test.func_3(test.func_1)的变化。

第二个片段的不同之处在于您不再将函数传递给func_3,而是直接调用类实例的func_1方法。

答案 1 :(得分:0)

在处理案例时,如果要在类外调用func_1()并将其视为常规函数而不是方法,则可以使用global。

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())
相关问题