semi colonic
avi.e.gross at gmail.com
avi.e.gross at gmail.com
Thu Feb 23 15:56:54 EST 2023
Grant,
I am not sure it is fair to blame JSON for a design choice.
Use of commas can be done many ways in many contexts.
One context is a sort of placeholder. Can you have a language where a
function has multiple arguments and you can skip some as in:
Func(a,b,c)
Func(a, b,)
Func(a,,)
Or even
Func(a,,c)
The missing arguments in such a language may be viewed as some sort of NULL
or take a default value as a possibility.
So what if you have a language where a list or tuple or other data structure
incorporates a similar idea when created or used. If I have a matrix and I
want every entry in row 4, meaning all columns, can I ask for mat[4,] and it
means something else than mat[4] which may return a vector instead of a
matrix?
There are tons of such ideas that are choices. Python allows a SINGLE comma
here but multiple are an error:
>>> a=1
>>> a
1
>>> a=1,
>>> a
(1,)
>>> a=1,,
SyntaxError: incomplete input
So why not allow MULTIPLE commas and ignore them? It is a choice!
Here is a scenario where a trailing comma is an error:
>>> a,b,,, = range(5)
SyntaxError: invalid syntax
>>> a,b,_,_,_ = range(5)
>>> a,b,*_ = range(5)
The way to deal here with more items is to use * in front of the last one to
gather any strays.
But as _ is simply reused in the middle example and meant to be ignored, why
do you need it if you would simply allow multiple commas? Short answer is
they did not choose to design it that way. The places in python that do
allow a trailing "," will allow only one. Beyond that, they assume you are
making an error. So if someone wants to make a list of 5 things in
alphabetical order but forgets a few, they cannot write:
mylist = [first, , third, , , ]
and then let the code run to be enhanced later with their reminder. What
they can do is write this:
mylist = [first,
#,
third,
#,
#,
]
The reminders are now simply well-placed comments.
Now we could debate the design of JSON and some enhancements people have
made for other more portable data structures. I think it reasonable that
they decided to stick to working with fully-formatted data structures and
guess what? If I make a list or tuple or other data structures in python
with a trailing comma, it is NOT stored that way and if you display it,
there is no trailing comma shown. It is fully JSON compatible in some sense:
>>> import json
>>> mynest = [1,2, [3, 4,], 5,]
>>> mynest
[1, 2, [3, 4], 5]
>>> json.dumps(mynest)
'[1, 2, [3, 4], 5]'
>>> json.dumps([1,2, [3, 4,], 5,])
'[1, 2, [3, 4], 5]'
>>> json.loads(json.dumps(mynest))
[1, 2, [3, 4], 5]
So when are you running into problems? Is it when reading something from a
file using a function expecting properly formatted JSON?
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of Grant Edwards
Sent: Thursday, February 23, 2023 2:28 PM
To: python-list at python.org
Subject: Re: semi colonic
On 2023-02-23, rbowman <bowman at montana.com> wrote:
> On Wed, 22 Feb 2023 18:25:00 -0800 (PST), Hen Hanna wrote:
>
>> i sometimes put extra commas... as:
>>
>> [ 1, 2, 3, 4, ]
>>
>> so it is (or may be) easier to add things later.
>
> That can bite you with things like JSON that aren't very forgiving.
Oh, how I hate that about JSON...
--
https://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list