Python Forum

Full Version: Pausing and returning to function?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The basic idea I have is something like this, but with actual python builtins:
def somefunc():
    print('Something')
    pause
    print('Other Thing')

somefunc()
somefunc()
Expected output:
Output:
Someting Other Thing
I used to think that yield could do this, but as I have found out, yield simply makes a generator. Is there a built in solution, or some other way to do this?
You have to have state stored somewhere. A generator is a normal way to do that, but does mean you have to call the iterator that it returns to get the data, not the function that creates the generator.

def somefunc():
    yield 'Something'
    yield 'Other Thing'


iterator = somefunc()
print(next(iterator))
print(next(iterator))