7 ms·
>f-strings What happened to "explicit is better than implicit"? "Only one way to do stuff"? One of the more baffling Python features if you ask me.
by Grue3 7y ago
>f-strings
What happened to "explicit is better than implicit"? "Only one way to do stuff"? One of the more baffling Python features if you ask me.
- sametmax 7y agoThis debate already took place. Go back to the python-idea mailing list posts about f-string for a complete back and forth on your point.
- kragen 7y agoIt took place three times actually; Ping proposed string interpolation in 2000 in PEP 215 https://www.python.org/dev/peps/pep-0215/ https://www.python.org/dev/peps/pep-0215/ and it was rejected, or rather simplified by Barry Warsaw in 2002 in PEP 292, which was actually implemented: https://www.python.org/dev/peps/pep-0292/ https://www.python.org/dev/peps/pep-0292/ and remains in modern Python. Barry's optimism that this would satisfy the demand for template strings was of course foolish, but it met with Guido's misguided approval at the time. Fifteen years later, it was proposed a third time as PEP 498 and finally got in in a sane form: https://www.python.org/dev/peps/pep-0498/ https://www.python.org/dev/peps/pep-0498/
- paganel 7y agoExactly, the f-string syntax looks so close to PHP’s way of handling strings that I’m really amazed that it managed to pass and be implemented. I mean, I don’t see that much of a difference between > name = ‘Jane’ > print(f”Hello, {name}”) and PHP’s: > $name = “Jane”; > echo “Hello, $name”;
- ChrisSD 7y agoWhy is looking similar to the PHP method inherently a bad thing?
- paganel 7y agoAbout 15 years ago when I was starting my life as a professional programmer PHP's way of handling strings was seen as inferior to stuff like: > name = 'Jane' > print 'Hello, %s' % (name) but I can't exactly reproduce all the discussions from back then. As for me personally I can see that I dislike the new > print(f'Hello {name}') format because my editor cannot instantly show me that {name} is in a fact a variable, and now that I wrote this down I remember that this was one of the complaints made against PHP's use of "Hello, $name" . But maybe newer editors do in fact recognize {name} as a Python variable, in any case it looks and feels "impure", for a lack of a better word.
- ChrisSD 7y agoVScode and PyCharm recognise variables in f-strings correctly. I'm not sure how "%s" is more pure than "{name}". They are both variables, only in the case of "%s" you add a level of indirection.
- wongarsu 7y agoPHP has many problems, but I always considered string handling one of PHP's strengths. Considering how PHP's string interpolation has gone mainstream I don't seem to be the only one: PHP: print("Hello, $name") print("Hello, ${getName()}) Python: print(f"Hello, {name}") C# Console.WriteLine($"Hello, {name}") Javascript Console.Log(`Hello, ${name}`) Ruby puts "Hello, #{name}" etc.
- ryl00 7y agoIs that really PHP's, though? It's been in shell scripts and perl for quite a while...
- wongarsu 7y agoThat's an interesting question. I couldn't find any summary of the history of string interpolation. The first version of PHP in '94 had it. Bash predates that by 5 years and Perl by another 2, but I'm not sure about the initial feature set of either of those. The first POSIX standard for command interpreters is from 1992. I suspect the feature comes from early versions of csh in the early 80s. However I think it's fair to credit PHP with introducing generations of programers to the idea.
- mturmon 7y agoBourne shells as early as Version 7 Unix (1979, [1]) had rather elaborate interpolation of variables into strings (i.e., not just ${foo} but ${foo?bar}, etc.). I'm reasonably sure that earlier versions of sh had some facility for this as well. So, it's not really a PHP innovation, although some people might have seen it first there. [1] https://www.in-ulm.de/~mascheck/bourne/v7/ https://www.in-ulm.de/~mascheck/bourne/v7/ -> "Parameter substitution"
- wongarsu 7y agoApparently Unix Version 6 (1975) used Thompson Shell which (provided the manuals are correct [1][2]) didn't support arbitrary variables (and even environment variables were not a thing back then). However for use in scripts $0, $1, etc were replaces with the script name and the arguments. 1: https://www.in-ulm.de/~mascheck/bourne/v6/ https://www.in-ulm.de/~mascheck/bourne/v6/ 2: https://etsh.nl/man/_tsh.1.html https://etsh.nl/man/_tsh.1.html
- qwsxyh 7y agof-strings are not the same way to do things but a different way. You can't explode a dictionary into an f-string to fill in the fields, for example, but you can in .format().
- shuzchen 7y agoYou seem to have forgotten "practicality beats purity"
- whalesalad 7y agof-strings is the right way to do this going forward, the rest will die off.