On Tue, 14 Mar 2023 at 11:37, Gary Herron <gher...@digipen.edu> wrote:
>
>
> On 3/13/23 2:26 PM, morp...@gmail.com wrote:
> > Hi.
> >
> > I was working in Python today, and sat there scratching my head as the
> > numbers for calculations didn't add up.  It went into negative numbers,
> > when that shouldn't have been possible.
> >
> > Turns out I had a very small typo, I had =- instead of -=.
> >
> > Isn't it unpythonic to be able to make a mistake like that?
> >
> > Regards,
> >
> > Morten
> >
>
> These all mean the same thing, but I don't see a good way to designate
> the second or third as an error.
>
>
> x = -5
> x=-5
> x =- 5
>

The second one isn't definitely an error, but the third is a failure
of style. Many style guides mandate, for instance, equal whitespace
either side of a binary operator. It's pretty straight-forward for a
program to tokenize your code the exact same way that Python would, so
it will interpret it thus:

NAME "x"
(whitespace " ")
OP "="
OP "-"
(whitespace " ")
NUMBER "5"

The whitespace parts aren't tokens in the normal sense, but worst
case, you can count positions. And since there's one space prior to
the "=" and none after, it violates the style rule, and can thus be
flagged.

(This is one reason that I detest autoformatters. You might notice the
autoformatter relayout your code into "x = -5", but if you don't spot
it right at that moment, there's a lower chance that it'll look like a
problem. OTOH, something that simply flags the error and leaves it for
you to fix, even if you don't notice it immediately, will bring this
line to your attention and let you wonder whether it's misformatted or
miscoded.)

Of course, all this is predicated on you actually putting whitespace
around your equals signs. If you write it all crunched together as
"x=-5", there's no extra clues to work with.

Linters and code reviewers can make use of all the available
information, including whitespace, to determine programmer intent.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to