简体   繁体   中英

Appropriate to use repeated function calls to loop through something (i.e. a list) in Python?

Lets say I have the following Python script:

def pop_and_loop():
    my_list.pop(0)
    my_func()

def my_func():
    #do something with list item [0]
    if my_list[0] finished_with:
        pop_and_loop()
    #continued actions if not finished with
    if my_list[0] finished_with:
        pop_and_loop()

my_list = [#list containing 100 items]
my_func()

Is this an appropriate setup? Because, am I not leaving each function call open in a way because its having to hold a marker at the position where I have left the function to go to another, so theoretically it is waiting for me to come back, but I'm never coming back to that one. Does this create problems and is there a different way you're meant to do this?

My actual script is more complicated than this, with loads of different functions that I need to call to whilst processing each item in the main list.我的实际脚本比这更复杂,在处理主列表中的每个项目时我需要调用大量不同的函数。 Essentially my question is whether I need to convert this setup into an actual loop. Bearing in mind that I will need to refresh the main list to refill it again and then loop through the same again. So how would I keep looping that?

Should I instead have:

my_list = []

def my_func(item):
    #do something with list item
    if item finished_with:
        return output
    elif item_finished_now:
        return output

while not len(my_list):
    while #there are items to fill the list with:
        #fill list

        for x in my_list:
            output = my_func(x)
            #deal with output and list popping here
    #sleep loop waiting for there to be things to put into the list again
    time.sleep(60)

Yours is simply an example of recursion.

Both the question and answer are borderline opinion-based, but in most cases you would prefer an iterative solution (loops) over recursion unless the recursive solution has a clear benefit of either being simpler or being easier to comprehend in code and in reasoning.

For various reasons, Python does not have any recursion optimizations such as tail call and creates a new stack frame for each new level (or function call). That, and more, are reasons an iterative solution would generally be faster and why the overhead of extra recursive calls in Python is rather large - it takes more memory for the stack and spends more time creating those frames. On top of all, there is a limit to the recursion depth , and most recursive algorithms can be converted to an iterative solution in an easy fashion.

Your specific example is simple enough to convert like so:

while my_list:
    while my_list[0] != "finished":
        # do stuff
    my_list.pop(0)

On a side note, please don't pop(0) and use a collections.deque instead as it's O(1) instead of O(N) .

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