Re: =- and -= snag

2023-03-15 Thread Morten W. Petersen
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
 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


Re: =- and -= snag

2023-03-14 Thread aapost




On 3/14/23 18:50, Rob Cliffe wrote:
On 14/03/2023 21:28, avi.e.gr...@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

2023-03-14 Thread rbowman
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

2023-03-14 Thread Keith Thompson
"Morten W. Petersen"  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 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.thompso...@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

2023-03-14 Thread Rob Cliffe via Python-list



On 14/03/2023 21:28, avi.e.gr...@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

2023-03-14 Thread avi.e.gross
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  On 
Behalf Of Morten W. Petersen
Sent: Tuesday, March 14, 2023 4:31 PM
To: Jon Ribbens 
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  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

2023-03-14 Thread Morten W. Petersen
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  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

2023-03-14 Thread Jon Ribbens via Python-list
On 2023-03-13, Morten W. Petersen  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

2023-03-14 Thread aapost

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

2023-03-13 Thread Thomas Passin

On 3/13/2023 9:47 PM, Chris Angelico wrote:

On Tue, 14 Mar 2023 at 12:38, Thomas Passin  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

2023-03-13 Thread Chris Angelico
On Tue, 14 Mar 2023 at 12:38, Thomas Passin  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

2023-03-13 Thread Thomas Passin

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

2023-03-13 Thread avi.e.gross
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  On
Behalf Of Morten W. Petersen
Sent: Monday, March 13, 2023 5:26 PM
To: python-list 
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

2023-03-13 Thread MRAB

On 2023-03-14 00:28, Gary Herron 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 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

2023-03-13 Thread Chris Angelico
On Tue, 14 Mar 2023 at 11:37, Gary Herron  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


Re: =- and -= snag

2023-03-13 Thread Gary Herron



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


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

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


=- and -= snag

2023-03-13 Thread Morten W. Petersen
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