[Python-Dev] (name := expression) doesn't fit the narrative of PEP 20

Tim Peters tim.peters at gmail.com
Fri Apr 27 17:54:32 EDT 2018


Wes, sorry, but I really don't follow what you're saying.  For example,

[Wes Turner <wes.turner at gmail.com>]
> Do not do this:
>
> x = 2
> if (x == 3) or (x := 3):
>    print(x)
>
> What do we call that mistake?

It displays 3 - while it appears to be silly code, there's nothing
about it that's undefined.  So I fail to see how showing that example
anywhere would do anyone any good.

You can do the same kind of thing today via, e.g.,

    class Bindable:
        def __init__(self, value):
            self.bind(value)

        def bind(self, value):
            self.value = value
            return value

        def __bool__(self):
            return bool(self.value)

        def __eq__(self, other):
            return self.value == other

        def __str__(self):
            return str(self.value)

Then:

>>> x = Bindable(2)
>>> if x == 3 or x.bind(3):
...     print(x)
3

And I wouldn't put that example anywhere in any docs either ;-)


More information about the Python-Dev mailing list