←back to thread

620 points tambourine_man | 2 comments | | HN request time: 0.429s | 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 #
1. jsnell ◴[] No.43749868[source]
It being a language feature gives you controlled access to the lexical scope, such that the template string can refer to variables by name rather than having to pass each value as a parameter. Doing it via parameters is repetitive and/or error-prone.
replies(1): >>43753190 #
2. linsomniac ◴[] No.43753190[source]
You CAN get access to the calling scope of a function. Something like:

    current_frame = inspect.currentframe()
    env = current_frame.f_back.f_locals.copy()
I did it in uplaybook so that you can do things like:

    for module in ['foo', 'bar', 'baz']:
        ln(path="/etc/apache2/mods-enabled", src="/etc/apache2/mods-available/{{ module }}.load")
This is an ansible-like tooling with a python instead of YAML syntax, hence the Jinja2 templating.