Remix.run Logo
unsnap_biceps 5 days ago

  I have a function I want to be a coroutine, but it has zero yield statements, so it is just a normal function?
  
  You can distinguish it from a normal Python function by putting if False: yield somewhere inside its body. Another common trick is to put a yield statement after the final return statement. Bit ugly but oh well.
I'm fairly unfamiliar with python and I don't quite understand what this is actually doing. Does it change anything in the execution or is it just to mark it in a way for IDEs to do something with?
kstrauser 5 days ago | parent [-]

Yes. If a function contains the yield statement, calling that function returns a generator instead of executing its body. For example, here's defining and calling a regular function:

  >>> def foo_function():
  ...     print('In a function!')
  ...     return
  ...
  >>> foo_function()
  In a function!
And here's defining and calling a generator:

  >>> def foo_generator():
  ...     print('In a generator!')
  ...     return
  ...     yield
  ...
  >>> foo_generator()
  <generator object foo_generator at 0x10321aa40>
  >>> next(foo_generator())
  In a generator!
  Traceback (most recent call last):
    File "<python-input-6>", line 1, in <module>
      next(foo_generator())
      ~~~~^^^^^^^^^^^^^^^^^
  StopIteration
Notice that the generator's body isn't evaluated until you consume the generator. StopIteration isn't actually an error in usual cases. It just says that the generator doesn't have any more values to return and has exited. For example, that's how Python's for-loop works:

  >>> for _ in foo_generator():
  ...     continue
  ...
  In bar!
Here it executes the generator's body (including the print() call) until it gets to the return statement. Because it's a generator, it returns as normal and then raises a StopIteration exception, which tells the loop to stop looping.