←back to thread

620 points tambourine_man | 1 comments | | HN request time: 0.612s | 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 #
dec0dedab0de ◴[] No.43752889[source]
It was easier than I thought, but also took longer than I wanted to. turns out the inspect module provides what you need to pull it off.

This dummy example splits a string that it was given, then if one of those values is in the callers context it saves those in self.context, and has an output function to assemble it all together. Obviously this example is not very useful, but it shows how a library could do this as a class or function without the user having to pass in locals().

  import inspect
  class MyXString:
      """ will split on whitespace and replace any string that is a variable name 
    with the result of str(variable)"""
        def __init__(self, string):
            self.string = string
            caller_locals = inspect.currentframe().f_back.f_locals
            self.context = {}
            for key in  set(string.split()):
                if key in caller_locals:
                    self.context[key] = caller_locals[key]
  
        def output(self):
            output = self.string
            for k,v in self.context.items():
                output = output.replace(k,str(v))
            return output
replies(1): >>43756878 #
1. nhumrich ◴[] No.43756878[source]
This looks like a performance nightmare, and would likely never have IDE integration. So I guess I was wrong. It could be a library. But will be much better supported officially.