jimbaker / fl-string-pep

PEP to add fl-string support to Python 3.10
0 stars 0 forks source link

sys._getframe() #2

Open gvanrossum opened 4 years ago

gvanrossum commented 4 years ago

Your README says

  • The callback variant cannot be derived from the raw string at runtime without using sys._getframe, because of the scope analysis. [...]

It's worse than that: if an interpolation references a nonlocal variable we'd have to walk the chain of closures -- but there's no object that represents that. If the referenced variable exists in a caller's frame that has been GC'ed (because the caller returned), there's no way to recover its value. Real f-strings discover the needed closures during compilation and make them available in cells.

jimbaker commented 4 years ago

Good point! Dynamic scoping (walking the frames) and lexical scoping (this static analysis) can overlap, but this is an important distinction. I will look into improving the README (likely the PEP itself) accordingly.