←back to thread

317 points est | 1 comments | | HN request time: 0.235s | source
Show context
oooooof ◴[] No.17448560[source]
What is it? The link points to a discussion more deep than I’m willing to read.
replies(10): >>17448567 #>>17448570 #>>17448571 #>>17448572 #>>17448575 #>>17448579 #>>17448584 #>>17448591 #>>17448617 #>>17448638 #
est ◴[] No.17448572[source]
It's a controversial PEP https://www.python.org/dev/peps/pep-0572/ which allows you to write Python like this:

    def foo():
        if n := randint(0, 3):
            return n ** 2
        return 1337


    [(x, y, x/y) for x in input_data if (y := f(x)) > 0]
replies(7): >>17448580 #>>17448633 #>>17448694 #>>17448731 #>>17448946 #>>17449000 #>>17449023 #
sluukkonen ◴[] No.17448731[source]
Would've making regular assignment an expression broken too much existing code?
replies(2): >>17448916 #>>17449059 #
sametmax ◴[] No.17449059[source]
It's a voluntary design choice since the beginning of Python to avoid the very common mistake of doing:

    while continue = "yes":
instead of:

    while continue == "yes":
Those mistakes introduce bugs that are hard to spot because they don't cause an immediate error, linters can hardly help with them and even a senior can make them while being tired.
replies(1): >>17453480 #
1. bluecalm ◴[] No.17453480[source]
I don't know about linters but GCC warns me about that every time I make that typo. They could just require parenthesis when assignment value is used as boolean.