3 ms·
It's not so simple. The full implementation has to do something like insert an AST into the right place, because {foo} can be an expression like f"{__import__('
by dalke 11y ago
It's not so simple. The full implementation has to do something like insert an AST into the right place, because {foo} can be an expression like f"{__import__('math').cos(len(s))}".
The Python tokenizer will pass an f-string to the AST builder, which has to pass the string to another tokenizer to generate the new AST. Because f-strings can contain f-strings, this process is recursive. The end result is a new AST that replaces the original f-string.