osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


It's certainly a contrived example. Actual code with such a mistake is
generally far more subtle.

The mistake is that it's assigning a value within a clause of a conditional
that won't be evaluated.

Oh well. I'll suffer the then worsened zig-zaggy eye movements in code
reviews caused by defining values at the end of expressions that reference
them which fit on a single line.

There are a number of bad examples for style guides in these threads.

:=

I wasn't aware of this switch, thanks!
http://coverage.readthedocs.io/en/latest/branch.html
coverage run --branch code.py



On Friday, April 27, 2018, Tim Peters <tim.peters at gmail.com> wrote:

> 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 ;-)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180427/6c3be8f9/attachment.html>