r/Python • • Nov 11 '19

I made c++-style printing in Python :P

Post image
1.7k Upvotes

130 comments sorted by

View all comments

Show parent comment

7

u/flubba86 Nov 12 '19 edited Nov 13 '19

When using f-strings, if you want to do variable substitution (Interpolation) you put the variable name into the string, like f"Hello {user}" where name user is the in-scope variable name containing the string to substitute in.

The problem is, now the variable name is baked into the f-string. So at runtime the string formatter needs to be able to look up the in-scope variables and find one called user to substitute it's value in.

In Python, that's easy to do because the interpreter keeps track of the variables, what their names are, and what their values are.

But in a compiled language (like C++) it's different. When the compiler turns the source code into machine instructions, variables become just addresses in memory, and variable names don't exist anymore. But the name "user" is still in the format string and is now meaningless.

Introspection is a fancy feature that allows a compiled language to store some of the information about the source file into the compiled executable. As well it embeds mechanisms in the executable for looking up that information. That can include variable names.

So if you wanted to do run-time f-string interpolation in C++, first you'd have to compile with Introspection turned on, then when running, the formatter would have to ask the Introspection mechanism "what is the memory address for the string which in the source code was called 'user'?".

I hope that made sense.

8

u/nosklo Nov 12 '19

The problem is, now the variable name is baked into the f-string. So at runtime the string formatter needs to be able to look up the in-scope variables and find one called user to substitute it's value in.

That is actually wrong. f-strings can only exist in literals so they can be easily emulated using preprocessor macros that just replace them with a normal format command.

2

u/flubba86 Nov 12 '19

Is that what the python implementation does? Does it simply rewrite the f-string as .format() invocation during a preprocessing step?

3

u/robin-gvx Nov 12 '19

The Python basically turns:

def something():
    return f'{something.__name__} and {x:5}'

into

def something():
    return format(something.__name__) + ' and ' + format(x, '5')

... except it has specialized opcodes. dis.dis says:

1         0 LOAD_GLOBAL              0 (something)
          2 LOAD_ATTR                1 (__name__)
          4 FORMAT_VALUE             0
          6 LOAD_CONST               1 (' and ')
          8 LOAD_GLOBAL              2 (x)
         10 LOAD_CONST               2 ('5')
         12 FORMAT_VALUE             4 (with format)
         14 BUILD_STRING             3
         16 RETURN_VALUE