python - Mutable Default Argument Returns None -
i have simple code finds paths using graph stored in dictionary. code exactly:
def find_path(dct, init, depth, path=[]): if depth == 0: return path next_ = dct[init] depth-=1 find_path(dct, next_, depth) if print path right before return path prints screen correct path (after initial depth of 5). however, value returned none. don't know what's going on!
why value of path right above return correct, yet returned path not want?
shouldn't
find_path(dct, next_, depth) be
return find_path(dct, next_, depth) # ^^^^ # return in python (unlike in say, ruby) have explicitly return value. otherwise none returned.
Comments
Post a Comment