TypeError: 'int' object is not callable - How do I identify what is causing this error?

TypeError: 'int' object is not callable - How do I identify what is causing this error?

如果我能收到一些关于为什么我会收到此类型错误的意见,那将是绝对了不起的。我已经调试了几个小时,但我无法在任何地方找到导致此函数中出现此特定错误的原因...:(

def a_plus_abs_b(a, b):
    if b < 0:
        f = sub(a, b)
    else:
        f = add(a, b)
    return f(a, b)

fintfloat,不是函数。你不能调用它。

当您尝试调用它时 f(a, b),您遇到了错误:

TypeError: 'int' object is not callable

您应该 return f 来避免这种情况。