Python递归函数缺少结果 [英] Python Recursive function missing results
问题描述
来自Python递归追加列表函数尝试递归获取与文件结构关联的权限列表.
Coming from Python recursively appending list function Trying to recursively get a list of permissions associated with a file structure.
我有这个功能:
def get_child_perms(self, folder, request, perm_list):
# Folder contains other folders
if folder.get_children():
# For every sub-folder
return [self.get_child_perms(subfolder, request, perm_list) for subfolder in folder.get_children()]
return folder.has_read_permission(request)
这将返回除包含其他文件夹的文件夹之外的所有结果.
That returns all the results except the folders that contain other folders.
folder <- Missing (allowed)
subfolder <- Missing (restricted)
subsubfolder <- Get this (restricted)
files
函数的输出是[对,错,错]
Output from function would be [True, False, False]
另一种情况是,A = 允许,R = 受限
another case would be, where A = allowed, R = restricted
folder A
subfolder A
subsubfolder R
files
files
subfolder R
files
subfolder A
subsubfolder A
files
files
subfolder A
files
files
输出将是[真、真、假、假、真、真、真]
Output would be [True,True,False,False,True,True,True]
推荐答案
基本问题是你只返回文件夹权限
,当文件夹没有任何孩子时,当它有孩子时,你不包括 folder.has_read_permission(request)
在您的返回结果中,这很可能会导致您出现问题.你需要做 -
The basic issue occurs you are only returning the folder permission
, when folder does not have any children , when it has children, you are not including the folder.has_read_permission(request)
in your return result , which is most probably causing you issue. You need to do -
def get_child_perms(self, folder, request, perm_list):
# Folder contains other folders
if folder.get_children():
# For every sub-folder
return [folder.has_read_permission(request)] + [self.get_child_perms(subfolder, request, perm_list) for subfolder in folder.get_children()]
return [folder.has_read_permission(request)]
这应该导致(未测试)-
This should result in (not tested) -
[folderperm [subfolderperm [subsubfolderperm]]
这篇关于Python递归函数缺少结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!