当我从 Python 中的 class 调用方法时出现 TypeError

TypeError when I call a method from a class in Python

当我在不带参数的 class 中调用一个方法时,我得到了这个结果:

TypeError: unbound method getAge() must be called with Human instance as first argument (got nothing instead)

我简化了我的真实问题。我有三个文件。

文件:Class.py

class Human:
    age = ""
    def getAge(self):
        return self.age

文件Get.py

from Class import *
    def getHuman():
        human = Human
        return human

文件init.py

from Get import *
from Class import *
@app.route("/human")
def humanAge():
    human = getHuman()
    return human.getAge()

我在文件 Get.py 中创建了 class 人类,并从中获取了 class。当我调用方法 getAge() 时,它说我必须使用 Human 作为第一个参数。为什么会这样?

当您尝试创建一个在 init.py 中使用 human = getHuman() 分配的实例时,您在 Get.py 中缺少括号,因此 human.getAge() 给您看到的错误:

def getHuman():
     human = Human() # <-  need to add ()
    return human`