News for hackers. Stuff that matters.
Re:Pattern matching accepted for Python by cjauvin on Tuesday February 09, 2021 @04:11PM (#26082422)
I understand your feeling and I would tend to agree for things in other domains.. but for a programming language, I find that if:

(1) It doesn't make the language less efficient

(2) The new feature is coherent with the rest of the language

(3) It doesn't make learning the language harder

(4) It brings joy in my life (as f-strings do)

Then.. it's ok :-)

Re:Pattern matching accepted for Python by chriswarbo on Tuesday February 09, 2021 @05:51PM (#26083575)
There are some 'invisible' downsides to adding new features, which often get overlooked:

- Tooling, code analysers, etc. have to be updated to understand the new features. Tools which are dead or "finished" may stop working.

- The possibility that someone might use a new feature can break guarantees that are relied upon by existing code. An obvious example would be adding try/catch to a language: in languages without this, a function call is guaranteed to either return control to us, or to run indefinitely. Libraries might rely on this to clean up file descriptors, etc. afterwards. Such libraries would be broken by the mere existence of try/catch; even if we never actually use it.

That said, I'm a big fan of pattern-matching, so it would be nice to have in Python, rather than crude approximations like '{pattern1: A, pattern2: B, pattern3: C, ...}[myVal]'

Re:Pattern matching accepted for Python by pansa2 on Tuesday February 09, 2021 @07:08PM (#26084277)
Pattern matching will definitely make Python harder to learn, though. It’s not particularly coherent with the rest of the language either - the match statement has been described as “a DSL contrived to look like Python, and to be used inside of Python, but with very different semantics” [0].

[0] https://discuss.python.org/t/gauging-sentiment-on-pattern-ma...

Re:Pattern matching accepted for Python by rement on Tuesday February 09, 2021 @04:46PM (#26082772)
I have never seen f-strings! It's my lucky day! https://xkcd.com/1053/

Time to refactor the project I am currently working on that is loaded with `str.format()`

Re:Pattern matching accepted for Python by matt_trentini on Tuesday February 09, 2021 @06:54PM (#26084168)
Take a look at flynt, a tool that can convert older string formatting code to the newer fstring style. I've found it very helpful!

https://github.com/ikamensh/flynt

Re:Pattern matching accepted for Python by aidos on Tuesday February 09, 2021 @07:02PM (#26084234)
Can confirm. I replaced 1.5k instances in our code base in seconds using flynt and it knew the ones that couldn't be swapped out directly. Really great tool. You only ever need it once in your life, but that one time it's amazing.
Re:Pattern matching accepted for Python by andrewem on Tuesday February 09, 2021 @07:51PM (#26084558)
Having not written Python in a long time until recently, I discovered fstrings. Are they the best choice for (nearly all) string formatting at least in new code? They seem to work pretty similarly to eg Ruby’s string interpolation.
Re:Pattern matching accepted for Python by ilovetux on Tuesday February 09, 2021 @06:07PM (#26083727)
Be aware that the only time this won't work is if you need to re-render the f-string with different context. For example you can do this:

    s = "this is {}"
    print(s.format("possible"))
but you cannot do that with f-strings because they are resolved on definition, so you could do something like this:

    for message in messages:
        print(f"The message is {message}")
But not this:

    s = f"The message is {message}"
    for message in messages:
        print(s)
This is the same drawback of JavaScript's Template Literals [0].

EDIT: Fix syntax in examples.

[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:Pattern matching accepted for Python by ZephyrBlu on Tuesday February 09, 2021 @05:50PM (#26083563)
f-strings are truly amazing. They're very painless. The only gotcha I've run into is accessing a dict from inside one. You need to watch out for your quotations :).
Re:Pattern matching accepted for Python by mumblemumble on Tuesday February 09, 2021 @06:57PM (#26084193)
My most recent project makes liberal use of `f"""..."""`
Re:Pattern matching accepted for Python by aldanor on Tuesday February 09, 2021 @07:29PM (#26084419)
Have you seen this? :)

    x, y = 1, 2
    print(f'{x=}, {y=}')
Re:Pattern matching accepted for Python by rement on Wednesday February 10, 2021 @09:28AM (#26089550)
I haven't but that is awesome. That is a much better syntax than just outputting data with `print(x, y)` and remembering which came first (when debugging of course).
Re:Pattern matching accepted for Python by kbenson on Tuesday February 09, 2021 @09:01PM (#26085002)
I find it both incredibly humorous and saddening to see Python devs excited about finally getting string interpolation.