【问题标题】:Call a method on a class instance (function in class) that calls other functions within that class调用类实例(类中的函数)上的方法,该方法调用该类中的其他函数
【发布时间】:2019-08-29 14:59:26
【问题描述】:

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

尝试在不将 func_1 传递给 func_3 的情况下调用它: "NameError: name 'func_1' is not defined"

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

将 func_1 传递给 func_3 并使用参数调用它: "NameError: name 'func_1' is not defined"

这是工作代码(没有类,只有一些函数:

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 进行编码时相同的结果。

【问题讨论】:

  • print(self.func_1())替换print(func_1)
  • 如果他经过test.func_1,他需要打电话给self.func_1()吗?
  • 不,但首先将func_1 作为参数传递是没有意义的......

标签: python python-3.x class


【解决方案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()

在第一个 sn-p 中,注意从 print(func_1)print(func_1()) 的变化。更重要的是,还要注意从test.func_3(func_1)test.func_3(test.func_1) 的变化。

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

【讨论】:

  • 你应该只显示第二个,为什么将同一类的方法作为参数传递?
  • 因为 OP 说他“希望获得与 [他] 在没有类的情况下编写代码时相同的结果”。不过,我完全同意你的看法,第一个选项表明缺乏对类的理解——但是,没有办法知道 OP 实际打算用这些函数做什么。也许他/她真的没有一个以函数为参数的函数?使用第二种方法func_3 只能对一个成员函数进行操作。
  • 我真的认为 OP 不需要将函数作为参数传递。更可能的是,他并不完全理解应该如何使用类和方法,这就是为什么我认为第一个选项令人困惑并且不会帮助他理解事情是如何工作的......
  • @user10987432 非常感谢您的时间和精力。它解决了我的问题并帮助我理解了类中函数的概念。
  • @olinox14 感谢您的 cmets,他们对我很有价值。
【解决方案2】:

玩转你的案例,如果你想在类外调用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())

【讨论】:

  • 感谢您的时间,解决方案和评论 Jourmaico,上面来自“user10987432”的解决方案是我想要实现的。
猜你喜欢
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
相关资源
最近更新 更多