【问题标题】:Python: printed object has a type but returned object is NoneType? [duplicate]Python:打印的对象有类型但返回的对象是NoneType?
【发布时间】:2021-03-30 03:42:35
【问题描述】:

我有一个函数,它返回一个包含 NumPy 数组和列表的元组。在函数结束时,我打印出数组和列表,看起来都正确。然后我打印他们的类型,这些看起来也正确。但是当我返回它们时,我得到一个 NoneType 错误。我很困惑为什么会这样。代码如下。 adjust_param 是一个辅助函数。 TypeError 在optimize_theta 的返回行中断言。

def adjust_param(R, delta, i, theta):
    thetaplus = theta.copy()
    thetaminus = theta.copy()
    thetaplus[i*2] += delta
    thetaplus[i*2+1] += delta
    thetaminus[i*2] -= delta
    thetaminus[i*2+1] -= delta    
    y = Remp(q_data, labels, R, num_samples, theta)
    yplus = Remp(q_data, labels, R, num_samples, thetaplus)
    yminus = Remp(q_data, labels, R, num_samples, thetaminus)
    if (yplus < y and yplus < yminus and yplus != -1):
        return thetaplus, yplus
    elif (yminus < y and yminus < yplus and yminus != -1):
        return thetaminus, yminus
    else:
        return theta, y
def optimize_theta(N, R, delta, i, theta, risk):
    if N == 0:
        print("Theta : " + str(type(theta)))
        print("= " + str(theta))
        print()
        print("Risk : " + str(type(risk)))
        print("= " + str(risk))
        return theta, risk
    else:
        theta_new, risk_new = adjust_param(R, delta, i, theta)
        if i == (len(theta)/2)-1:
            #print("N = " +  str(N-1))
            #print("theta = " + str(theta))
            risk_copy = risk.copy()
            risk_copy.append(risk_new)
            optimize_theta(N-1, R, delta, 0, theta_new, risk_copy)
        else:
            optimize_theta(N, R, delta, i+1, theta_new, risk)

输出:

Theta : <class 'numpy.ndarray'>
= [0.85885111 0.86066499 0.47482528 0.13555158 0.87249245 0.02604654
 0.2906744  0.34618303]

Risk : <class 'list'>
= [0.6273510217403618]

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-307-8b43b528fee2> in <module>
----> 1 theta, risk = optimize_theta(N, R, delta, 0, theta0, [])

TypeError: cannot unpack non-iterable NoneType object

任何见解将不胜感激。谢谢!

【问题讨论】:

  • 您的else 部分没有明确返回任何内容,因此它返回None

标签: python list typeerror numpy-ndarray nonetype


【解决方案1】:

您必须在 optimize_theta 的 else 部分中显式返回结果。

def optimize_theta(N, R, delta, i, theta, risk):
    if N == 0:
        print("Theta : " + str(type(theta)))
        print("= " + str(theta))
        print()
        print("Risk : " + str(type(risk)))
        print("= " + str(risk))
        return theta, risk
    else:
        theta_new, risk_new = adjust_param(R, delta, i, theta)
        if i == (len(theta)/2)-1:
            #print("N = " +  str(N-1))
            #print("theta = " + str(theta))
            risk_copy = risk.copy()
            risk_copy.append(risk_new)
            return optimize_theta(N-1, R, delta, 0, theta_new, risk_copy)
        else:
            return optimize_theta(N, R, delta, i+1, theta_new, risk)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2013-03-14
    相关资源
    最近更新 更多