Mailing List Archive

=- and -= snag
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

--
I am https://leavingnorway.info
Videos at https://www.youtube.com/user/TheBlogologue
Twittering at http://twitter.com/blogologue
Blogging at http://blogologue.com
Playing music at https://soundcloud.com/morten-w-petersen
Also playing music and podcasting here:
http://www.mixcloud.com/morten-w-petersen/
On Google+ here https://plus.google.com/107781930037068750156
On Instagram at https://instagram.com/morphexx/
--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
On 3/13/23 2:26 PM, morphex@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


Dr. Gary Herron
Professor of Computer Science
DigiPen Institute of Technology

--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
On 2023-03-14 00:28, Gary Herron wrote:
>
> On 3/13/23 2:26 PM, morphex@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 third one could be picked up as suspicious by a linter due to its
unusual spacing.
--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
On Tue, 14 Mar 2023 at 11:37, Gary Herron <gherron@digipen.edu> wrote:
>
>
> On 3/13/23 2:26 PM, morphex@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
RE: =- and -= snag [ In reply to ]
Morten,

Suggesting something is UNPYTHONIC is really not an argument I take
seriously.

You wrote VALID code by the rules of the game and it is not a requirement
that it guesses at what you are trying to do and calls you an idiot!

More seriously, python lets you do some completely obscure things such as
check whether some random object or expression is truthy or not. There is no
way in hell the language, as defined, can catch all kinds of mistakes.

Now some languages or their linters have chosen to provide warnings of code
that may be valid but is often an error.

Consider:

x = 1
y = 0
x = y

Do I want to rest x to the value of y? Maybe. Or do I want the interpreter
to print out whether x == y perhaps?

Well what if the third line above was

x == y

Is that too a warning?

To add to the confusion some languages have an ===, :=, +=, -=, /=, |= or
oddities like %=% and many of these are all variations on meanings vaguely
related to equality before or after ...

So, no, it is not only not unpythonic, in my opinion, but quite pythonic to
let the interpreter interpret what you wrote and not know what you meant.

Is there possible a flag that would require your code to use spaces in many
places that might cut down on mistakes? There could be and so your example
of something like "new =- old" might be asked to be rewritten as "new = -
old" or even "new = (-old)" but for now, you may want to be more careful.

I do sympathize with the problem of a hard to find bug because it LOOKS
RIGHT to you. But it is what it is.

Avi

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Morten W. Petersen
Sent: Monday, March 13, 2023 5:26 PM
To: python-list <Python-list@python.org>
Subject: =- and -= snag

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

--
I am https://leavingnorway.info
Videos at https://www.youtube.com/user/TheBlogologue
Twittering at http://twitter.com/blogologue
Blogging at http://blogologue.com
Playing music at https://soundcloud.com/morten-w-petersen
Also playing music and podcasting here:
http://www.mixcloud.com/morten-w-petersen/
On Google+ here https://plus.google.com/107781930037068750156
On Instagram at https://instagram.com/morphexx/
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
On 3/13/2023 9:07 PM, Chris Angelico wrote:
> 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.

This is the kind of thing that unit tests can catch.

--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
On Tue, 14 Mar 2023 at 12:38, Thomas Passin <list1@tompassin.net> wrote:
>
> On 3/13/2023 9:07 PM, Chris Angelico wrote:
> > 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.
>
> This is the kind of thing that unit tests can catch.
>

Maybe, but that's quite orthogonal. The linter would highlight the
exact line of code with the odd whitespace; a unit test would merely
point out that the overall behaviour is incorrect, which would have
been no further information beyond what the OP already knew (the
numbers weren't adding up).

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
On 3/13/2023 9:47 PM, Chris Angelico wrote:
> On Tue, 14 Mar 2023 at 12:38, Thomas Passin <list1@tompassin.net> wrote:
>>
>> On 3/13/2023 9:07 PM, Chris Angelico wrote:
>>> 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.
>>
>> This is the kind of thing that unit tests can catch.
>>
>
> Maybe, but that's quite orthogonal. The linter would highlight the
> exact line of code with the odd whitespace; a unit test would merely
> point out that the overall behaviour is incorrect, which would have
> been no further information beyond what the OP already knew (the
> numbers weren't adding up).
>
> ChrisA

*This* time the OP happened to know. People in the thread have been
discussing how to pick this kind of mistake with linters or what have
you. Even with a linter, whether or not this would have been picked up
depends on how it has been configured.

Really, the only defense against these kind of potential mistakes or
typos is not to use constructions that may be more likely to get wrong
(or be typoed). In this particular case, that would probably be too
great a limitation for most of us. But the general principle is a good
one. Douglas Crockford wrote a book on using just the better parts of
Javascript (JavaScript: The Good Parts - rather dated by now but still
worth the reading).

Of course, anyone can have a brain blip on any given day!

--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
On 3/13/23 17:26, Morten W. Petersen wrote:

> 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?
>

That is why I tell Alice it is always best to stay positive and use
x-=-1 to avoid situations like that. *shrugs*

--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
On 2023-03-13, Morten W. Petersen <morphex@gmail.com> wrote:
> 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?

Why would it be? How could it be? Mandating white-space between
operators would be unpythonic.

That's nothing anyway - yesterday I had an issue in TypeScript which
confused me for a while which turned out to be because 1 + 1 = 11.
(I thought the whole point of TypeScript was to prevent things like
that...)
--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
Hoi.

After reading the replies, I think some script / linter etc. is the right
thing to do. What's the best linter for Python out there that covers this
as well?

At first glance I thought =- was a new assignment operator, and this is
what seemed unpythonic to me, to have two operators that were so similar.

-Morten

On Tue, Mar 14, 2023 at 5:31?PM Jon Ribbens via Python-list <
python-list@python.org> wrote:

> On 2023-03-13, Morten W. Petersen <morphex@gmail.com> wrote:
> > 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?
>
> Why would it be? How could it be? Mandating white-space between
> operators would be unpythonic.
>
> That's nothing anyway - yesterday I had an issue in TypeScript which
> confused me for a while which turned out to be because 1 + 1 = 11.
> (I thought the whole point of TypeScript was to prevent things like
> that...)
> --
> https://mail.python.org/mailman/listinfo/python-list
>


--
I am https://leavingnorway.info
Videos at https://www.youtube.com/user/TheBlogologue
Twittering at http://twitter.com/blogologue
Blogging at http://blogologue.com
Playing music at https://soundcloud.com/morten-w-petersen
Also playing music and podcasting here:
http://www.mixcloud.com/morten-w-petersen/
On Google+ here https://plus.google.com/107781930037068750156
On Instagram at https://instagram.com/morphexx/
--
https://mail.python.org/mailman/listinfo/python-list
RE: =- and -= snag [ In reply to ]
There seem to be a fundamental disconnect here based on people not understanding what can happen when spaces are optional. Yes, I have had my share of times I found what I programmed was not quite right and been unhappy but the result was mostly learning how to not not not not do that next time and follow the rules.

When I write:

Var = -----1

The result of five minus signs in a row is not a new long operator called "-----". It is five instances of "-" and looks more like:

Var=-(-(-(-(-1))))

You can even throw in some plus signs and they are effectively ignored.

It is no different than some other operators like:

Var = not not not not True

So the "=-" case is not a single operator even as "-=" is a single operator.

If I add another negative symbol to the above, I have two operators with a binary operator of "-=" that may be implemented more efficiently or use a dunder method that handles it and then a unary "-" operator.

>>> Var = 1
>>> Var -=-1
>>> Var
2

Yes, I can sympathize with humans who think the computer should do what they meant. They may also not like scenarios where they mess up the indentation and assume the computer should guess what they meant.

But life is generally not like that. Whenever you are in doubt as to how something will be parsed especially given how many precedence levels python has and so on, USE PARENTHESES and sometimes spaces.

If you type "Var - = 5" you get a syntax error because it sees two different operators that do not mesh well. If you type "Var = - 5" you get a result that should make sense as the minus binds to the 5 and negates it and then the assignment is done. If you leave the space between symbols out, then "-=" evaluates to a new operator and "=-" evaluates to two operators as described.

There are lots of trivial bugs people find including truly simple ones like subtracting instead of adding or using a floating point number like 5.0 when you meant to use an integer or forgetting that 5/3 and 5//3 do somewhat different things. People often substitute things like bitwise operators and the computer does what you tell it. Many languages have doubled operators like "&" versus "&&" that do different things. And if you want to make a list and instead of using square brackets use curly brackets, in python, you get a set! There are so many places you can mess up.

ALL languages have such features where people can and do make mistakes and sometimes cannot easily find them. Add in mistakes where different parts of a program use the same variable name and one changes it out and the other gets a confusing result. Simply put, lots of stuff is not only legal but often useful and even when a linter or other program sees what might be a mistake, it will often be wrong and something you wanted done.

Consider another such arena in the lowly spelling Checker that keeps telling me things are spelled wrong because it does not know Schwarzenegger is a name or that Grosz and Groß are valid variations on the spelling of my name. Imagine what it does when I write in any language other than English. One solution has been to have it add such words to a dictionary but that backfires when it allows words as valid even though in the current context, it is NOT VALID. So some such programs allow you to designate what dictionary/language to use to check a region such as a paragraph. Some may transition to being closer to grammar checkers that try to parse your sentences and make sure not only that a word is a valid spelling but valid given what role it plays in the sentence!

Computer languages are both far simpler and yet weirder. You need to use them in ways the documentation says. But when you consider various ideas about scope, you can end up with it needing to know which of perhaps many copies of the same variable name is being referenced. So if you wrote a program where you had a local variable inside a function that you changed and you ASS U ME d you could use that variable outside the scope later and another variable already exists there with the same name, how is it supposed to know you made a mistake? How does it know you wanted a deep copy rather than a reference or shallow copy?

There are so many other examples that the short answer to many questions is something like THAT IS THE WAY IT IS. Don't do that!

I doubt anyone would like it if computer programs were written with multiple layers of redundancy so that many errors could be detected when all the parts do not align along with a checksum. Human languages that do something similar are, frankly, a royal pain. I mean once you have a gender and a tense and a singular/plural, for example, everything else nearby, such as adjectives, must be adjusted to have the right form or endings to match it. That may have been great when it was hard to hear a speaker from the back of the theater so the redundancy helped offer possible corrections but these days just makes the language much harder to learn. I vastly prefer a designed language like Esperanto to any human ones that evolved into monstrosities.

We have lots of computer languages to choose from and some are designed to protect you from yourself to the extent that I simply have no interest in programming in them. Some take quite a bit of work to even get it to compile so you can then look to see if there are any bugs not caught. Python is towards the other end of a spectrum where the idea is to let you write programs easier. There are people now trying to in some ways ruin the usability by putting in type hints that are ignored and although potentially helpful as in a linter evaluating it, instead often make it harder to read and write code if required to use it. Some programmers like freedom and others like dependability. Of course, freedom is relative and after you have been mugged a few times by your errors, you may decide it is worth getting some protection.

So, consider having code reviews where fresh eyes may spot your errors.







-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Morten W. Petersen
Sent: Tuesday, March 14, 2023 4:31 PM
To: Jon Ribbens <jon+usenet@unequivocal.eu>
Cc: python-list@python.org
Subject: Re: =- and -= snag

Hoi.

After reading the replies, I think some script / linter etc. is the right
thing to do. What's the best linter for Python out there that covers this
as well?

At first glance I thought =- was a new assignment operator, and this is
what seemed unpythonic to me, to have two operators that were so similar.

-Morten

On Tue, Mar 14, 2023 at 5:31?PM Jon Ribbens via Python-list <
python-list@python.org> wrote:

> On 2023-03-13, Morten W. Petersen <morphex@gmail.com> wrote:
> > 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?
>
> Why would it be? How could it be? Mandating white-space between
> operators would be unpythonic.
>
> That's nothing anyway - yesterday I had an issue in TypeScript which
> confused me for a while which turned out to be because 1 + 1 = 11.
> (I thought the whole point of TypeScript was to prevent things like
> that...)
> --
> https://mail.python.org/mailman/listinfo/python-list
>


--
I am https://leavingnorway.info
Videos at https://www.youtube.com/user/TheBlogologue
Twittering at http://twitter.com/blogologue
Blogging at http://blogologue.com
Playing music at https://soundcloud.com/morten-w-petersen
Also playing music and podcasting here:
http://www.mixcloud.com/morten-w-petersen/
On Google+ here https://plus.google.com/107781930037068750156
On Instagram at https://instagram.com/morphexx/
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
On 14/03/2023 21:28, avi.e.gross@gmail.com wrote:
> TThere are people now trying to in some ways ruin the usability by putting in type hints that are ignored and although potentially helpful as in a linter evaluating it, instead often make it harder to read and write code if required to use it.
+1
Whenever I see code with type hints, I have to edit them out, either
mentally, or physically, to understand what the code is actually doing. 
It's adding new syntax which I'm not used to and don't want to be forced
to learn.
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
> On 3/14/23 18:50, Rob Cliffe wrote:
> On 14/03/2023 21:28, avi.e.gross@gmail.com wrote:


Type hints are actually situationally quite useful (though yes, kind of
hard to understand when you first come across their use, largely because
of things like Union). And I wouldn't recommend their use in all
situations because maintaining them is frustrating.

xmlschema uses them extensively though and I appreciate them. The module
does what I need where all the other offerings failed, and parsing the
source because of the subject matter would have been harder to quickly
grok without them.

Alternatively, I spent quite a while on the subject of pep 232 (before
finding the pep) trying to understand it because intuitively I felt like
there was something I must be missing. Turns out there isn't. And what
my thoughts were assuming was the intent, are discussed under "Future
Directions", but when seeing it laid out like that, subjectively the
dissenting conclusion of "It opens the way to mind abuse." is more
correct. "What if I launch a virtual machine inside a virtual machine
inside a virtual machine inside a virtual machine, what happens?"...

If I want to have a grouping of minor functionality or attributes on a
callable, I can get that with creating a class that defines __call__.

I would say 232 is fine for what it is (I don't have to use it, and in
many cases it probably is), but the only wide use I know of that I have
come across is in the standard library ElementTree:
# For tests and troubleshooting
register_namespace._namespace_map = _namespace_map

Which I hate that variable and it's design with a passion. lol.

And exactly, the more a language starts policing my ability to be
flexible in my trying to find a creative solution to a problem, the less
I want to use it. Sometimes to have something useably beautiful to
interact with requires something painfully complex under the surface.
Hiding and avoiding all complexity away so you never have to see it or
make mistakes prevents you from growing.

Anyway, the 20th rule to The Zen of Python is:
Don't believe your own bullshit *shrug*


--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
"Morten W. Petersen" <morphex@gmail.com> writes:
> 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?

Very early versions of C (around 1975 or so, before K&R1 was
published) actually used "op=" for compound assignment operators, so
`x =- 2` would subtract 2 from x. It was changed to "=op" (`x -= 2`)
precisely to avoid this ambiguity that programmers kept running
into (people were less generous with whitespace back then).

As late as the late 1990s, I used a compiler (VAXC) that still
recognized the old-style compound assignment operators, though I
think it warned about them.

I thought "Pythonic" was more about how you write code than about the
design of the language. But designing a language syntax so typos are
likely to be syntax errors rather than valid code with different
semantics is an interesting challenge.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */
--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
On Tue, 14 Mar 2023 22:15:34 GMT, Gilmeh Serda wrote:

> On Mon, 13 Mar 2023 22:26:20 +0100, Morten W. Petersen wrote:
>
>> numbers for calculations didn't add up. It went into negative numbers,
>> when that shouldn't have been possible.
>
> We have all written code that makes us wonder why the compiler even
> bothered with it and didn't give up on the first line.

gcc does have a flag so it will stop on the first error instead of
wandering around aimlessly spitting out 100 errors because you missed a
curly bracket. Sometimes it's useful.

--
https://mail.python.org/mailman/listinfo/python-list
Re: =- and -= snag [ In reply to ]
I don't remember making such a mistake ever before, but it might have
happened. -= is a convenient operator so I've probably used it a lot.

Anyway, I found that flake8 flagged this as

E225 missing whitespace around operator

and it's the only linter of pylint, flake8 and pyflake that detects this.

Regards,

Morten

On Wed, Mar 15, 2023 at 5:32?AM Gilmeh Serda
<gilmeh.serda@nothing.here.invalid> wrote:

> On Mon, 13 Mar 2023 22:26:20 +0100, Morten W. Petersen wrote:
>
> > numbers for calculations didn't add up. It went into negative numbers,
> > when that shouldn't have been possible.
>
> We have all written code that makes us wonder why the compiler even
> bothered with it and didn't give up on the first line.
>
> --
> Gilmeh
>
> If this fortune didn't exist, somebody would have invented it.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


--
I am https://leavingnorway.info
Videos at https://www.youtube.com/user/TheBlogologue
Twittering at http://twitter.com/blogologue
Blogging at http://blogologue.com
Playing music at https://soundcloud.com/morten-w-petersen
Also playing music and podcasting here:
http://www.mixcloud.com/morten-w-petersen/
On Google+ here https://plus.google.com/107781930037068750156
On Instagram at https://instagram.com/morphexx/
--
https://mail.python.org/mailman/listinfo/python-list