简体   繁体   中英

I have a method / function in python in which i'm trying to get a return value which i can then use as a variable in another function

So i'm trying to learn Python and at the same time write some code to help myself out with some mundane tasks. so i have a function which works just fine if i use the print statement to display just what i need, but when i replace the " print " statement for a " return " i only get the first element in the list, i know its because "return" causes the function to exit, but i have no idea how i can get this to return the rest of the elements. i have tried lots of things and been now reading for hours :( and still getting no further. objective: To read all the "*.common" files in a directory and then compare them with a text file containing some names, and if they match return the name of the *.common file so i can use it in another function as a variable. code i have thus far

# This method gets the list of common files....
def get_common_files():
    path = (config_home)
    files = []

    for r, d, f in os.walk(path):
        for file in f:
            if '.common' in file:
                files.append(file)

    for f in files:
        return files

new_file = get_common_files()

def build_default_manifest():
    for line in fileinput.FileInput(new_manifest):
        for name in new_file:
            if line.strip() in name:
                print(name) # works as it should and displays all that i need
                return name  # only shows me the first element 


good_name = build_default_manifest()
print(good_name)  

i hope i've provided all that i need to but if not please feel free to comeback to me...

When you do this:

for f in files:
    return files

This is not quite right. There are 2 problems here. First, what this is doing is just returning files -- the whole thing, one time. Putting a loop body around the return doesn't really make sense. Likely you meant to type return f , to iterate over the results.... but that leads to a second problem...

The return statement will run the first time it is encountered, causing the function to end, and that's it. So just changing what is returned to return f still doesn't quite work: it will return only the first value in files .

This is actually really close to a slightly more advanced pattern called a generator. If you replace the above with this:

for f in files:
    yield f

then your function should work. Explaining why it works is not quite as easy, but this question has some good answers that should help.

Just out of curiosity, did you write this from scratch yourself, or did you follow an example from somewhere? I'm just a bit surprised at how close you got to the generator pattern, apparently unintentionally, and assuming you were not aware of it before.

def build_default_manifest():
    for line in fileinput.FileInput(new_manifest):
        for name in new_file:
            if line.strip() in name:
                yield name  

good_name = build_default_manifest()
for name in good_name:

    print(name)

like Z4-tier said, sounds like you need a generator.

you can use each element in good_name for a function call like normal.

Perhaps I'm missing something here but isn't your for loop (for r, d...) already creating your list called 'files'? So, why not just change:

for f in files:
    return files

to

return files

which should just return the entire list?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM