Python recursively appending list function

自闭症网瘾萝莉.ら 提交于 2019-12-24 14:43:09

问题


Having issues with a recursive function that should be relatively simple to do, but can`t seem to get right.

I have a folder structure with folders that can contain other folders, images or files. Associated with each folder there are permissions. I want to have my function recursively build a list of the permissions that are associated with each folder.

I have a function has_read_permission(request) that returns True if the folder has permissions and False if not

I have built a function like so:

def get_child_perms(self, folder, request, perm_list):
        # Folder contains other folders
        if folder.get_children():
            # For every sub-folder
            for subfolder in folder.get_children():
                return perm_list.append(self.get_child_perms(subfolder, request, perm_list))
        else:
            # If folder doesn't have sub-folders containing folders
            return [folder.has_read_permission(request)]

I keep getting None

Given folders like so:

Folder (allowed) - Wont check this one
|_First Folder (allowed)
| |_First sub Folder (restricted)
| | |_File
| | |_File
| | |_Image
| |__Second Sub Folder (allowed)
|_Second Folder (allowed)

Then running get_child_perms() would return [True,False,True,True] or even [True, [False, True], True]

EDIT

Disregard edit -> asked other question Python Recursive function missing results

Changed abit,

def get_child_perms(self, folder, request, perm_list):
        if folder.get_children():
            for subfolder in folder.get_children():
                perm_list.append(self.get_child_perms(subfolder, request, perm_list))
            return perm_list
        else:
            return [folder.has_read_permission(request)]

getting:

[[True], [...], [True], [...], [...], [True], [True], [True], [True], [True], [True], [True], [True], [...], [True], [...]]

 Admin
 -Folder 1
   - Files
 -Folder 2
   - Files
 -Folder 3
   - Files 
 -Folder 4
   - SubFolder 1
      -SubSubFolder 1
         - Files
      - Files
   - SubFolder 2
      - SubSubFolder 2
           - Files
      - Files
 -Folder 5
   - SubFolder 3
       - Files
   - SubFolder 4
       - Files
   - SubFolder 5
       -Files
   - Files
 -Folder 6
   - Files
 -Folder 7
   - SubFoler 6
       - Files
   - Files
 -Folder 8
   - Files

回答1:


As pointed out append is an inplace operation that returns None, you can avoid using append at all by returning a list comp:

def get_child_perms(self, folder, request, perm_list):
    # Folder contains other folders
    children =  folder.get_children()
    if children:
        # For every sub-folder
        return [self.get_child_perms(subfolder, request, perm_list)
                for subfolder in children]
    return [folder.has_read_permission(request)]

You don't need an else either as you can only return once from the function.




回答2:


for subfolder in folder.get_children():
    perm_list.append(self.get_child_perms(subfolder, request, perm_list))
return perm_list

.append is in place. It does not return anything.So you are getting None




回答3:


In-place list methods return None to remind you that they operate in place.

Instead of:

return permlist.append(self.get_child_perms(...  # etc.

Try:

permlist.append(self.get_child_perms(...
return permlist

Edited to add: As other comments/answers have pointed out, you'll want to return outside your for loop if you want it to complete.



来源:https://stackoverflow.com/questions/32102420/python-recursively-appending-list-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!