简体   繁体   English

Python:return语句仍未从函数返回任何内容

[英]Python: return statement still returns none from function

I've looked at all of the other "Returns none" questions on here and none of them seem to solve my problem. 我在这里查看了所有其他“不归还”问题,但这些问题似乎都无法解决我的问题。

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

The functions them selves work fine, the output is like so: 他们自己选择的功能运行良好,输出如下:

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

ror is the correct value, but why does it not get returned when I have it written right there return ror ?? ror是正确的值,但是当我在正确的位置写return ror时为什么不返回它? Doesn't make any sense to me 对我没有任何意义

You need to return it here too 您也需要在这里退货

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)

In date_initialize , you need to return the function that is returning the value that you want. date_initialize ,您需要返回正在返回所需值的函数。 Explicitly, change your call from 明确地,从

rate_of_return_calc(date_new_i, date_i)

to

return rate_of_return_calc(date_new_i, date_i)

Your first call, to date_initialize , does not return anything. 您第一次调用date_initialize不会返回任何内容。 Therefore, when you call rate_of_return_calc , you receive the value and then throw it away. 因此,当您调用rate_of_return_calc ,您会收到该值,然后将其丢弃。 You need to return it to pass the value along to your main function. 您需要返回它,以将值传递给您的主函数。

You need to return the value in date_initialize too: 您还需要在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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM