Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对调用该类中其他函数的类实例(类中的函数)调用方法_Python_Python 3.x_Class - Fatal编程技术网

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

Python 对调用该类中其他函数的类实例(类中的函数)调用方法,python,python-3.x,class,Python,Python 3.x,Class,我在Python的一个类中工作,它有3个函数。 函数1和函数2独立运行,但函数3应打印从函数1返回的内容。 我将func_1的函数名传递给func_3,但我无法得到正确的结果。当我创建一个名为“test”的类的实例时,我不能调用test.func_3,我尝试了所有我知道的或能在internet上找到的东西 尝试在未将func_1传递到func_3的情况下调用它: “名称错误:未定义名称‘func_1’” 将func_1传递到func_3并在没有参数的情况下调用它: “TypeError:func

我在Python的一个类中工作,它有3个函数。 函数1和函数2独立运行,但函数3应打印从函数1返回的内容。
我将func_1的函数名传递给func_3,但我无法得到正确的结果。当我创建一个名为“test”的类的实例时,我不能调用test.func_3,我尝试了所有我知道的或能在internet上找到的东西

尝试在未将func_1传递到func_3的情况下调用它: “名称错误:未定义名称‘func_1’”

将func_1传递到func_3并在没有参数的情况下调用它: “TypeError:func_3()缺少1个必需的位置参数:'func_1'”

将func_1传递到func_3并使用参数调用它: “名称错误:未定义名称‘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,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 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()
或者像这样:

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
方法。

如果您想调用
func_1()
在类之外,并将其视为正常函数而不是方法

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

print(func_1)
替换为
print(self.func_1())
他是否需要调用
self.func_1()
如果他通过了
test.func_1
?没有,但首先作为参数通过
func_1
是没有意义的……你应该只显示第二个参数,为什么作为参数通过的兴趣是同一类的方法?因为OP说他“希望获得与[他]编码时相同的结果”它没有阶级”但是,我完全同意你的看法,第一种方法显示了对类的理解不足-但是,没有办法知道OP实际打算对这些函数做什么。也许他/她真的不是一个以函数为参数的函数?第二种方法
func_3
只能对e成员函数。我真的不认为OP需要传递一个函数作为参数。更可能的是,他不完全理解类和方法应该如何使用,这就是为什么我认为第一个选项令人困惑,不能帮助他理解事情是如何工作的…@user10987432非常感谢你的时间和努力。它修复了我的pr问题并帮助我理解类中函数的概念。@olinox14感谢您的评论,它们对我很有价值。感谢您的时间、解决方案和评论Jourmaico,上面来自“user10987432”的解决方案正是我想要实现的。