+ 2
Is there any way such that we can continue working in a function even after using return feature
8 Respostas
+ 6
If you are okay with workarounds, you could choose yield instead of return.
With yield, you'll get a generator object that preserves its status, so that next time you call it, the body picks off where it stopped last time.
Strange demonstration:
def greetEveryone():
print('Hello', end=' ')
yield
print('World')
yield
gen = greetEveryone()
next(gen)
next(gen)
Probably there's a much simpler way for whatever you want to do.
If you give us more details, we can keep looking together.
+ 5
A generator function creates a generator object that remembers its current state.
Using next, code is executed up to the next yield, as long as you haven't reached the bottom of the function body (in this example you can call next two times).
greetEveryone() creates a *new* object of that sort, so the process starts from the top.
Your first example passes a new generator object to next each time, so you'll get the first yield value of two separate objects.
In the second version, you *store* the generator object, so you can iteratively call next on it and get further down within the body.
+ 4
Only if you have a finally block. Still thinking, but I'm almost sure it's the only option...
+ 1
Something tells me is just conditional statement you need to get pass your problem.
But the information you provided isn't even enough to help you.
Better still provide a link to your code.