4 ms·
I do love the Python F-string syntax, however, it shows a weakness of Python as it has been maturing over the years. The original Zen of Python was (PEP 20) Th
by oxfordmale 6y ago
I do love the Python F-string syntax, however, it shows a weakness of Python as it has been maturing over the years. The original Zen of Python was (PEP 20)
There should be one-- and preferably only one --obvious way to do it.
There are many ways to format a string, and F-Strings are yet another way. String formatting is not the only area where the original ZEN no longer applies. Of course this is part of a language natural evolution, however, it does make the language more confusing for new starters. Syntax sugar coating Python may also distract from solving more systematic problems, like the lack of proper multithreading in Python.
- akvadrako 6y agoI feel that python has been becoming less and less zen for a long time, starting with the async stuff and most recently with the walrus operator. I used to like it not because it was a great language but because it had a low barrier to entry for casual programmers. Now it's just like a worse Ruby with a lot of momentum from the early years.
- qayxc 6y agoI don't understand. Why exactly is having features that you don't need to use a barrier to entry? Casual programmers can use the same Python that 2.x used - with very little (yet IMO logical) differences. You can get started just as easily and use/learn (or don't) features as you need/wish.
- Majromax 6y agoCasual programmers, especially those learning Python as a first programming language, aren't well-placed to know which language features they need. If they're learning from multiple sources and tutorial series A teaches Python 2-style %-formatting, B teaches str.format, and C teaches f-strings, then the novice programmer will learn all of these equivalent features. This also shows up when the novice programmer wants to understand other people's code. If there is only one way to do things, then all common examples of a task should use that one way. If there are multiple ways (as here), then common examples (or an open source project of interest) may use any combination of them. To understand the code, the novice programmer needs to understand all of the right ways to do something rather than one of them.
- qayxc 6y ago> If they're learning from multiple sources and tutorial series A teaches Python 2-style %-formatting, B teaches str.format, and C teaches f-strings, then the novice programmer will learn all of these equivalent features. Isn't the key though, that they aren't in fact equivalent features at all? %-formatting doesn't allow for mapping names or changing the order of variables. f-strings are just a shorter version of .format() and there's no conceptual difference in usage. > If there is only one way to do things, then all common examples of a task should use that one way. If there are multiple ways (as here), then common examples (or an open source project of interest) may use any combination of them. Unfortunately, despite all noble efforts of avoiding this, Python is already full of these. This is true for any sufficiently powerful language. That's why there's idiomatic ways (here: "the Pythonic way") of doing things and "freestyling": x = [] for i in range(4): if i % 2 == 0: x.append(i) y = [] for i in range(0, 4, 2): y.append(i) z = [i for i in range(4) if i % 2 == 0] r = [i for i in range(0, 4, 2)] s = [] i = 0 while i < 4: # or: while (i < 4): s.append(i) i += 2 # or: i = i + 2 t = [] i = 0 while True: t.append(i) i += 2 if i >= 4: break # x, y, z, r, s, and t will contain the same values Every one of the snippets above showcases a different way of doing the exact same thing in Python. Some are idiomatic, others ugly, some are outright bad code, yet all are perfectly valid Python. How would a novice programmer know which of the above is "good" code and what is the preferred way to do it? A Python tutorial aimed at kids [1] never even mentions "range()", list-comprehensions, "+=", etc. yet enables children to write games in Python. A Python intro for non-programmers [2] on the other hand shows both for- and while-loops as well as "range()". So I'd argue that this is a non-starter as even the very basics as taught by recommended sites from the Python website itself [3] differ greatly in scope. So I [edit]don't[/edit] find f-strings to be a big deal, especially since string formatting is a rather advanced topic anyway and beginners would stick with for i in [1, 2, 3, 4, 5, 6, 7]: print("The number", i, "is", "even" if i % 0 == 0 else "odd") anyway instead of messing with f-string, %, or "format()" - whoops! - accidentally introduced yet another way of doing it :D [1] http://www.letslearnpython.com/learn/ http://www.letslearnpython.com/learn/ [2] https://thepythonguru.com/python-loops/ https://thepythonguru.com/python-loops/ [3] https://wiki.python.org/moin/BeginnersGuide/NonProgrammers https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
- deleted 6y ago[deleted]
- qayxc 6y ago> There should be one-- and preferably only one --obvious way to do it. Yeah, and then - as the language matures - so do its users. It's a common dilemma that cannot be fixed. You have the early adopters who grew up with the language and at some point got stuck in their ways (e.g. Python 2.x). Replacing day-one features with new ones feels like using a new language to those who used the language for decades. The more mature (read: older and more widespread) a programming language becomes, the harder it is to introduce breaking changes. String interpolation is one such area and due to the nature of the language such an important and integral part that it's becomes almost impossible to change. As someone who never touched Python until very recently, I never even considered using anything but f-string formatting; I acknowledge that other (legacy) options exist, but outside of reading other people's code, I simply ignore them. To new users who started with Python 3.x the obvious way to do it is f-strings and only language veterans switching to 3.x are affected by that feeling. IMHO nothing changed. 2.x uses print without parentheses and the %-operator. 3.x requires parenthesis and prefers f-strings.
- kevincox 6y agoI think this is actually improving the Zen of Python. Before my coworkers were pretty split between % and .format(). Now everyone uses fstrings. So while there is one more way to format strings now it seems that everyone has agreed that this is the "one correct way" to do it.
- dunefox 6y agoIt's the opposite of "only one obvious way to do it".