[英]Python recursive function not returning value
這是我的第一個問題,因此如果我寫的太多了,我表示由衷的歉意,並非常感謝您提前查看。
我編寫了一個函數,如果有必要,應該調用自身x次,如果失敗,則不返回任何內容。 但是,當函數看起來成功時,它仍然返回None。
背景:我有許多目錄,代表2009年的月份。但是,並非所有月份都存在,因此,如果是這種情況,我想獲取上個月,請檢查該月份的目錄是否存在,以及是否請不要在MOST的6個月后繼續返回一個月。
在下面,您會在date_tag中看到09月,在我的測試案例中,該月不存在。 08或07都不會。因此,該函數應返回06,但應返回None。
import pdb
import generate_months_module
import os
date_tag = '2000/2000/2009/09'
tree_location = '/Users/kelly/Documents/projects/komm/data/directory-tree/'
def iterateOverMonths(date_tag, x):
if x <= 0:
return_string = 'no dates found'
return return_string
else:
new_date = generate_months_module.handleDateShifts(date_tag)[1]
print '\tNEW DATE after calling handleDateShifts' + new_date
full_path = tree_location + '/' + new_date
if checkDirectoryExistance(full_path) == True:
print '\t'+ full_path + ' is a real path'
return full_path
else:
print 'dir does not exist'
iterateOverMonths(new_date, x-1)
def checkDirectoryExistance(dir_path):
"check if a directory exists, return true or false"
if os.path.isdir(dir_path) == True:
return True
else:
return False
print iterateOverMonths(date_tag, 6)
generate_months_module.handleDateShifts應該只獲取上個月並返回。 (這在其他測試用例中也有效,因此我高度懷疑問題出在這里!)
那么我的輸出是:
6
NEW DATE after calling handleDateShifts2000/2000/2009/08
dir does not exist
5
NEW DATE after calling handleDateShifts2000/2000/2009/07
dir does not exist
4
NEW DATE after calling handleDateShifts2000/2000/2009/06
/Users/kelly/Documents/projects/komm/data/directory-tree/2000/2000/2009/06 is a real path
returning full path
None
當我在“ return full_path”之前使用pdb.set_trace()時,似乎該函數被再次調用,盡管IF子句為True,因此覆蓋了我想返回的“ full_path”變量。
為什么不返回路徑“ / Users / kelly / Documents / projects / komm / data / directory-tree / 2000/2000/2009/06”?
如果您應該對此感興趣並想要重新創建它,則handleDateShifts函數如下所示(我很抱歉,這有點混亂):
def handleDateShifts(corpus_date_string):
"get background corpus date strings ALSO call this function if month does not exist and need to go back even further"
century, decade, year, month = corpus_date_string.split('/')
if month == '01' or month == '02':
#handle date boundaries which can affect year, decade and cent
background_mo_1 = '11'
background_mo_2 = '12'
millenium_shift = re.search('[1-9][0][0][0]$', year)
century_shift = re.search('[1-9][0][0]$', year)
decade_shift = re.search('[1-9][0]$',year)
if century_shift or millenium_shift:
century = int(year) - 100
decade = int(year) - 10
year = int(year) - 1
elif decade_shift:
decade = int(year) - 10
year = int(year) - 1
elif not decade_shift and not century_shift:
year = int(year) - 1
background_1_string = str(century) +'/'+ str(decade) +'/'+ str(year) +'/'+ str(background_mo_1)
background_2_string = str(century) +'/'+ str(decade) +'/'+ str(year) +'/'+ str(background_mo_2)
else: #the cent/dec/year can stay the same
background_mo_1 = int(month) - 2
background_mo_2 = int(month) - 1
if len(str(background_mo_1)) == 1:
background_mo_1 = '0' + str(background_mo_1)
if len(str(background_mo_2)) == 1:
background_mo_2 = '0' + str(background_mo_2)
background_1_string = str(century) +'/'+ str(decade) +'/'+ str(year) +'/'+ str(background_mo_1)
background_2_string = str(century) +'/'+ str(decade) +'/'+ str(year)+'/'+ str(background_mo_2)
return background_1_string, background_2_string
您在此分支中不返回任何內容(結果丟失):
else:
print 'dir does not exist'
iterateOverMonths(new_date, x-1)
如果函數運行過程沒有顯式return <smth>
,則不return <smth>
None
值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.