Lists And Extra Commas at end

Avi Gross avigross at verizon.net
Tue Dec 24 16:48:26 EST 2019


Marco,

Python is used by some in an interactive mode but also in more of a batch mode such as on a server. The former can possibly see a SyntaxWarning. Do you want that as a default or something you set when you start the Python Interpreter or perhaps a command within it? I note a brief search to see about Warnings in Jupyter keeps talking about how to turn warnings off!  My point is that warnings about what is not a real problem tend to make people ignore all warnings.

Let me switch gears to the terminal comma situation. Unlike many languages, Python decided a dangling comma is perfectly allowable in many situations, perhaps all.

>>> a=[1,2,3,]
>>> a
[1, 2, 3]

The above does not work for an empty comma at the beginning or middle or a double comma at the end. It is clear why they allowed this as it makes some things easier like building up a list in a loop and not needing to figure out the special case when you are done and suppressing the last comma. Since a function argument list is just a list in Python, the following works fine:

def testink(a,b,): pass

And, of course, you can use the same dangling comma in making a tuple, dictionary or set and who knows where else.

So, is that a feature you want warnings about? After all, a dangling comma may simply mean you left something out and meant to add later? 

As I see it, languages have tradeoffs and nobody will be completely satisfied so you choose and hope. But, warnings you can turn on an off do indeed make sense. But at what level? If the main interpreter has to do all kinds of testing even for a VALID use of the language so it can warn, that is extra overhead each time it is run. I would prefer something less intrusive. Some languages have a way of saying "use strict" (such as JavaScript or Perl) or a "use warnings" that tells to interpreter to warn about things. Since Python did not go this route, with perhaps a tad of that regarding the __future__ setting, I figured a good place to do this is in a Lint type of program that can be set to look for things that are not so much errors as places to check to make sure things are OK. You would not run that every time, just when the code has been changed.

And, if you look for a list of places people make mistakes that are often maddening to trace, there are so many. Would you like a warning that multiple NAMES are almost the same except for capitalization or that you have long_variable_1_of_2 as well as long_variable_2_of_2 which only differ by a tad deep in the middle?

At some point, you have to take the training wheels off. Not clear what that point is, sometimes, so I try to only drive vehicles with 4 or six tires, LOL!

-----Original Message-----
From: forwardedby at e4ward.com <forwardedby at e4ward.com> On Behalf Of Marco Sulla
Sent: Tuesday, December 24, 2019 4:03 PM
To: Avi Gross <avigross at verizon.net>
Cc: Python-list.python.org-mail.python.org.marco.sulla.e4ward.com at jq38efsu937.reply.e4ward.com
Subject: Re: Lists And Missing Commas

On Tue, 24 Dec 2019 at 19:05, Avi Gross via Python-list <python-list at python.org> wrote:
> There are some lint programs that check your code and supply warnings 
> and I see some languages have the option to generate warnings when the 
> two strings are on the same line. I wonder if a Python lint does that. 
> It may at least warn of this usage in time to check the code and put back the comma.

IMHO it's not something that should be delegated to linter, but to the AST compiler itself. A SyntaxWarning should be displayed.

One time I spent HOURS because I forgot one single comma in a list of strings. After that, my life is changed. I'm no more an ingenuous, little boy, that trusted the stacktrace.



More information about the Python-list mailing list