4 ms·
Exactly, 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
by paganel 8y ago
Exactly, 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 8y agoWhy is looking similar to the PHP method inherently a bad thing?
- paganel 8y 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 8y 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 8y 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 8y agoIs that really PHP's, though? It's been in shell scripts and perl for quite a while...
- wongarsu 8y 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 8y 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 8y 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
- kragen 8y agoString interpolation was there in the first versions of the Bourne shell, and I suspect the pre-Bourne shell in PDP-7 Unix in 1970 or so, but of course programmers have been generating strings by pasting together bits of constant and variable strings for a lot longer than that; Church defined the λ-calculus in terms of string interpolation in the 1930s, before there were even any computers. Computer-generated printed letters were already a commonplace occurrence in the US in the 1950s (at least if references in the fiction of the era are to be believed), and those are of course built with string interpolation. Around that time, assembly languages became the first languages to claim to make computer programming obsolete (because at the time "programming" meant programming in binary), and rather quickly some of them developed macro capabilities — before 1960, I think. Macro assemblers typically do their macro magic with string interpolation, even today. High-level languages of the time commonly used template strings, like those in (1964?) BASIC's PRINT USING statement, though FORTRAN's "edit descriptors" used a non-interpolation-based syntax. COBOL's "edited picture" feature is a kind of string interpolation, but I have no idea when it was added to COBOL. In the 1970s string interpolation really got going with languages like sh, cpp, and m4, which was popularized on many platforms by the "Software Tools" book (which in a sense is a slow buildup to the presentation of m4); m4 is Turing-complete entirely through string interpolation (more so even than Tcl decades later). I suspect Perl had string interpolation from the beginning (1988), given its shell roots, but I don't have a copy of Perl 1 to test with. For an overview of modern languages' string-interpolation syntax, I suggest https://www.rosettacode.org/wiki/String_interpolation_%28included%29 https://www.rosettacode.org/wiki/String_interpolation_%28inc.... Hope this helps!