Hacker News new | past | comments | ask | show | jobs | submit login

>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.




This 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.


It took place three times actually; Ping proposed string interpolation in 2000 in PEP 215 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/ 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/


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”;


PHP 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.


Is that really PHP's, though? It's been in shell scripts and perl for quite a while...


That'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.


String 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_%28inc....

Hope this helps!


Bourne 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/ -> "Parameter substitution"


Apparently 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/

2: https://etsh.nl/man/_tsh.1.html


Why is looking similar to the PHP method inherently a bad thing?


About 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.


VScode 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.


f-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().


You seem to have forgotten "practicality beats purity"


f-strings is the right way to do this going forward, the rest will die off.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: