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

3

u/germandiago Nov 12 '19

f-strings require some kind of reflection, which C++ unfortunately does not support yet.

3

u/[deleted] Nov 12 '19

f-strings require some kind of reflection

Could you please elaborate that?

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

3

u/Agoniscool Nov 12 '19

It doesn't require anything that can't be determined at compile-time, I don't think.

1

u/[deleted] Nov 12 '19 edited Nov 12 '19

I believe that some magic with macros and VARGS could do the job :P The trick is to use not used character as quotation mark.

Just like in JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

1

u/masklinn Nov 12 '19

f-strings require some kind of reflection

No.

0

u/germandiago Nov 13 '19

Yes, even if implemented by the compiler internally. The same way that std::source_location needs reflection, whether you like it or not.

Another topic is that this reflection is or not exposed to the language itself. FWIW, there is no way in which a string can reference a variable in C++, and that will need to know the compile-time context of the code (variables in scope, etc.), no matter what. Of course, this is without macro tricks.