In the documentation for the Python standard library module contextvars, it is stated that:
Context Variables should be created at the top module level and never within closures.
However, I am unclear about the meaning of this statement. While many online tutorials mention it, none provide an explanation. Could you please provide an example where a context variable is created within a closure, and explain the potential problems it may cause? Is it related to GC?
Context objects hold strong references to context variables which prevents context variables from being properly garbage collected.