< expLog

Coroutine Objects

Coroutine Objects

The object maintains a link to the actual code to be executed, as well as the state of execution by keeping the frame around. This is what makes it possible to "pause" and "resume" a coroutine when out-of-band mechanisms return.

Listing out the non-dunder methods to see what's available with the object.

instance = hello_world()
print([(type(getattr(instance, attribute)), attribute) for attribute in instance.__dir__() if not attribute.startswith("__")])

Comparing the attributes to confirm that the code is shared

instance_a = hello_world()
instance_b = hello_world()
Traceback (most recent call last):
  File "<string>", line 17, in __PYTHON_EL_eval
  File "<string>", line 3, in <module>
  File "/tmp/python-YkQlQ2", line 1, in <module>
    instance = hello_world()
               ^^^^^^^^^^^
NameError: name 'hello_world' is not defined
>>>