简体   繁体   中英

How can I call a function from itself while avoiding a recursion error in python?

I want to run a function with a while loop in it, and when that while loop breaks I want to execute some code and then run the function again. Currently my code looks like this:

def function1():
    while var1 != var2:
        dostuff
    else:
        dootherstuff
        function1() #call the function again

While I'm almost certain this is the wrong way to do it, as I get recursion depth exceeded errors every so often, I have no idea how else I would do it. Can someone clear this up please?

You can wrap it in another loop:

while True:
    while var1 != var2:
        dostuff
    else:
        dootherstuff

Python is not designed for deep recursion: use a functional language for that.

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