4 ms·
How does those `interpolate()` methods reach into the surrounding context to get values of the variables?
by scotty79 3y ago
How does those `interpolate()` methods reach into the surrounding context to get values of the variables?
- masklinn 3y agoThey don't, a string template literal is parsed into a StringTemplate object (including the result of evaluating each template fragment), and interpolate() is a method on that.
- scotty79 3y agoInterpolate method accesses callsite context to extract values of the variables named in template. How?
- masklinn 3y agoI assume it just parses the string templates (at compile-time) and extracts the expressions being templated in, that's not exactly high-flying tech, cpython manages to do it.
- scotty79 3y agoOh, right. The values seem to come from the context of where template was parsed and are kept inside StringTemplate object (.values()).