Python: return 语句仍然 returns none 来自函数

Python: return statement still returns none from function

我查看了此处的所有其他 "Returns none" 问题,其中 none 似乎可以解决我的问题。

rates = []
for date in unformatted_returns: # Please ignore undefined variables, it is redundant in this context
    if date[0] >= cutoff_date:
        date_i = unformatted_returns.index(date)
        r = date_initialize(date[0], date_i)
        print "r is returned as:", r
        rates.append(r)
        print date[0]
    else:
        continue

def date_initialize(date, date_i):
        print " initializing date configuration"
        # Does a bunch of junk
        rate_of_return_calc(date_new_i, date_i)

def rate_of_return_calc(date_new_i, date_i):
        r_new = unformatted_returns[int(date_i)] # Reverse naming, I know
        r_old = unformatted_returns[int(date_new_i)] # Reverse naming, I know
        if not r_new or not r_old:
            raise ValueError('r_new or r_old are not defined!!')
            # This should never be true and I don't want anything returned from here anyhow
        else:
            ror = (float(r_new[1])-float(r_old[1]))/float(r_old[1])
            print "ror is calculated as", ror
            return ror

他们自己的功能运行良好,输出如下:

initializing date configuration
('2014-2-28', u'93.52')
ror is calculated as -0.142643284859
r is returned as: None
2015-2-2
>>> 

ror 是正确的值,但是为什么当我把它写在那里 return ror 时它没有返回?对我来说没有任何意义

date_initialize 中,您需要 return 函数 returning 您想要的值。明确地,将您的电话从

更改为
rate_of_return_calc(date_new_i, date_i)

return rate_of_return_calc(date_new_i, date_i)

您的第一个电话 date_initialize 没有 return 任何内容。因此,当你调用 rate_of_return_calc 时,你接收到值,然后将其丢弃。您需要 return 它将值传递给您的主函数。

你也需要return这里

def date_initialize(date, date_i):
        print " initializing date configuration"
        # Does a bunch of junk
        return rate_of_return_calc(date_new_i, date_i)

您需要return date_initialize 中的值:

def date_initialize(date, date_i):
    print " initializing date configuration"
    # Does a bunch of junk
    return rate_of_return_calc(date_new_i, date_i)