=- and -= snag

avi.e.gross at gmail.com avi.e.gross at gmail.com
Tue Mar 14 17:28:10 EDT 2023


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 at python.org> On Behalf Of Morten W. Petersen
Sent: Tuesday, March 14, 2023 4:31 PM
To: Jon Ribbens <jon+usenet at unequivocal.eu>
Cc: python-list at 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 at python.org> wrote:

> On 2023-03-13, Morten W. Petersen <morphex at 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



More information about the Python-list mailing list