since python is dynamically evaluated, i can’t imagine the garbage collector being smart enough to be able to gc massive_local_object before the returned lambda is not referenced anymore, that is, the local function context does not have to be sustained any longer.
so if i had to return only a few of the locals in a function context, it would probably be better to do so with regular data structures then functions that utilize the local function context, so that unneeded local objects can be gc?
Well, actually, it is! There’s a difference between “fast locals” and “cell variables” in CPython; anything declared nonlocal and anything referenced by a nested function is put into a closure cell rather than a fast local.
The one that isn’t needed past the immediate function return is a fast local, so it’ll be disposed of; the one that’s needed for the lambda function is a closure cell, set with STORE_DEREF.
ye i suppose the interactive shell has always been quite fast with gc’ing, worst case you throw in a big numpy allocation and it starts gc’ing (eVeN ThOuGh iT Is nOt gUaRaNtEeD)