←back to thread

620 points tambourine_man | 1 comments | | HN request time: 0.207s | source
Show context
sashank_1509 ◴[] No.43749826[source]
Why does this need to be a language feature. This could just be a separate library, we could use brackets instead of a letter before a string. I fear, Python is going down the path of C++
replies(7): >>43749868 #>>43749921 #>>43749980 #>>43750107 #>>43750832 #>>43751165 #>>43751480 #
nhumrich ◴[] No.43751165[source]
This feature actually can't be a library. It needs to be able to preserve the string before the variables are passed in, which a library would be unable to do because f-strings immediately replace all the values. The whole premise of t-strings is to know which values where "hard coded" and which ones are variables. A library can't possibly know that. And a function call wouldn't have access to the local variables. The only way to do this without language support is to pass `locals()` into every function call.
replies(1): >>43752292 #
dec0dedab0de ◴[] No.43752292[source]
Now you have me wondering how difficult it would be to have a class that takes a string and parses through globals to find the context it was called from. maybe causing an exception and abusing a traceback? or maybe we just find our own objectid.... gahh I have to try it now, but I'm setting a timer.
replies(2): >>43752889 #>>43752977 #
zephyrfalcon ◴[] No.43752977[source]
You don't have to go through all the globals, you just have to get the caller's namespace, which is fairly simple. See e.g. https://stackoverflow.com/a/6618825/27426

For this reason, I think it's not true that this absolutely had to be a language feature rather than a library. A template class written in pure Python could have done the same lookup in its __init__.

replies(1): >>43753132 #
1. dec0dedab0de ◴[] No.43753132[source]
That is exactly what I ended up doing:

https://news.ycombinator.com/item?id=43752889