Are there any commands, methods, functions… to allow the manipulation of the CALL STACK in python. For example if a recursive function was looking for a specific value during its execution and found the value and needs to return the value but there are pending returns in the call stack is there a way to remove (not using return) such as pop, to remove them off the CALL STACK at the programmers discretion?
What do you have against return
?
You see my return to my main is on the bottom of the call stack, but piled on top are still unresoved recussive returns which I want to eliminate NOT by returning, but by removing.
Returning only continues the process but I have already found what I am looking for.
I don’t know about your desired way, but you could maybe use raise
, and catch it in your main.
I am not familiar with raise or catch maybe that is what I am looking for I will check on this. But yes, another way to solve my issue as your reply is hinting is to raise an exeption and get out. BUT i need to get out with my answer, return to the main, AND make sure I have not left a stack full of data.
If the raise or the catch clear off or delete the values left on the stack that would be great, but I don’t know how I could get back to the main with my answer if the stack is gone.
That said, I will investigate the raise and catch - thanks
Small demo, quickly finds and prints the index where gold is found:
def find(xs, x, i):
if xs[i] == x:
raise StopIteration(i)
find(xs, x, i + 1)
find(xs, x, i - 1)
def main():
xs = ['sand'] * 100 + ['gold']
try:
find(xs, 'gold', 0)
except StopIteration as e:
print(e.value)
main()
This might work. I see where you are going with this. I will give it a try! Get back to you soon. Thanks!
IT WORKED!!! Thank You !!!