recursion - Recursive function returning none in Python -
this question has answer here:
- i expect 'true' 'none' 1 answer
- python recursion list returns none 1 answer
i have piece of code, reason when try return path, none instead:
def get_path(dictionary, rqfile, prefix=[]): filename in dictionary.keys(): path = prefix+[filename] if not isinstance(dictionary[filename], dict): if rqfile in str(os.path.join(*path)): return str(os.path.join(*path)) else: get_path(directory[filename], rqfile, path)
is there way solve this? in advance.
you need return recursive result:
else: return get_path(directory[filename], rqfile, path)
otherwise function ends after executing statement, resulting in none
being returned.
you want drop else:
, return @ end:
for filename in dictionary.keys(): path = prefix+[filename] if not isinstance(dictionary[filename], dict): if rqfile in str(os.path.join(*path)): return str(os.path.join(*path)) return get_path(directory[filename], rqfile, path)
because if rqfile in str(os.path.join(*path))
false
end function without return
well. if recursing in case not right option, returning none
not, need handle edgecase too.
Comments
Post a Comment