The following question shows how to create a closure cell object, in order to programmatically construct functions with closures.
However, there's a chicken-and-egg problem here where I need to create the cells to create the function, but I may not be able to finalize the values I want the cells to have until after the function is created. (As a mad example, what if I want to put the function itself in one of its cells?)
Is there a way to set the cell_contents of a cell? I tried assigning to it, but I get an AttributeError claiming cell_contents isn't writable!
EDIT: I just realized that cell_contents is writable in the latest version of Python3 (3.7), albeit not in the latest pypy (3.6) version, which I'm using.