Most active commenters
  • masklinn(3)

←back to thread

620 points tambourine_man | 18 comments | | HN request time: 0.001s | source | bottom
1. enescakir ◴[] No.43749266[source]
Not sure about introducing yet another string prefix. Between f-strings, raw strings, and i18n stuff, it’s already getting crowded. Curious how readable this will be in large codebases.
replies(3): >>43749334 #>>43749489 #>>43749515 #
2. wodenokoto ◴[] No.43749334[source]
I'm of the opposite opinion. Let's set the prefixes free!

    from sql import sql

    query = sql"SELECT user_id, user_name FROM {user_table_versioned} WHERE user_name = {user_name}"
replies(4): >>43749474 #>>43749545 #>>43749548 #>>43749927 #
3. dmurray ◴[] No.43749474[source]
How would this be different from a function sql() that operates on one of these new t-strings?

The syntactic sugar of changing it from sql(t"...") doesn't seem particularly valuable. The novel thing about t-strings is that they change the parsing at compile-time.

replies(3): >>43749506 #>>43749648 #>>43749903 #
4. mulmboy ◴[] No.43749489[source]
Are there string prefixes for i18n stuff?
replies(1): >>43749599 #
5. stavros ◴[] No.43749506{3}[source]
It wouldn't be different, but it would be more convenient because we no longer have to count the number of %s, you'd put the variable inline.
replies(1): >>43749558 #
6. albert_e ◴[] No.43749515[source]
"Yet another" is not my main worry

The concept of prefixes itself feels a little deviant from readable code that is close to human language -- which is the spirit of Python

replies(3): >>43749659 #>>43749755 #>>43749968 #
7. mcintyre1994 ◴[] No.43749545[source]
This is how JavaScript does it with tagged template literals.

Your sql there would just be a function that receives the array of strings/values and returns whatever.

8. masklinn ◴[] No.43749548[source]
This was considered and rejected: https://peps.python.org/pep-0750/#arbitrary-string-literal-p...
9. masklinn ◴[] No.43749558{4}[source]
That's... already the case of t-strings?
replies(1): >>43749570 #
10. stavros ◴[] No.43749570{5}[source]
Yes, that's my point. The GP was already talking about a t-string.
replies(1): >>43749609 #
11. Biganon ◴[] No.43749599[source]
They're probably talking about the convention of using _ as an alias for `translate`
12. masklinn ◴[] No.43749609{6}[source]
dmurray was comparing a hypothetical sql"..." with sql(t"..."). There are no %s either way.
13. Timwi ◴[] No.43749648{3}[source]
> The syntactic sugar of changing it from sql(t"...") doesn't seem particularly valuable.

It's valuable because:

- IDEs could then syntax-highlight SQL inside of SQL strings and HTML inside of HTML strings

- You can't accidentally pass an HTML string to your SQL library

14. Timwi ◴[] No.43749659[source]
The single letter f or t does make it unnatural to read, but if it were sql"..." or html"...", I think that would help with that.
15. empiko ◴[] No.43749755[source]
Additionally, it will probably be confusing that it is called a t-string but it is actually a constructor for a Template object and not string at all. I would rather see a new special term `template` than this.
16. wodenokoto ◴[] No.43749903{3}[source]
It’s different from a function the same way f”” is different from f(“”) and t”” is different from t(“”)

There’s nothing stopping you from building a Python function that parses a string looking for {} and then searching globals for those variables. And you can extend that to also do some code execution and formatting.

To me the real sugar of f-strings is that the editor knows that it’s a template and not just a string. Expanding this to having SQL and regex syntax highlighting, linting and code formatting inside my Python code is a pretty cool prospect.

17. dsego ◴[] No.43749927[source]
This is what JS does with tagged template literals. https://github.com/dsego/sql_tag
18. toxik ◴[] No.43749968[source]
Should have been a keyword.

    a = template "foo {bar}"
As should raw and format.