Comma is not as simple as you think

Posted by Afsal on 14-Jan-2022

Last a day a friend of mine shared a post related say “5% of 666 Python Repositories have comma typos (including Tensorflow, PyTorch, Sentry, and V8)”. So I have decided to write a post on what is the problem that a typo comma can cause to your repo

Missing a comma: 

Let us check what happens when we miss a comma, normally we use comma in list, tuple, dictionary, etc.

Let consider the example

data = [

    "Apple"

    "Ball",

    "Cat"]

When we print this expected result will be an array with 3 elements, But the result is

['AppleBall', 'Cat']

Why ?

When we miss a comma between two Apple and Ball the python will consider this as a string concatenation that is the reason why AppleBall became the first element for the array

Adding extra comma:

Let us check what if we add an extra comma 

Let consider the example

a = 2,
print(type(a))
<class 'tuple'>

If we add an extra comma then python will consider this as a tuple. This will result in your code crash.

How can we avoid this:

These types of bugs are hard to find unless an exception is raised at runtime. One thing you can do proper code review before committing and during the code review.

Hope you have learned something from this post. Please share your suggestions and topics to discuss with afsal@parseltongue.co.in

Reference

https://blog.devgenius.io/5-of-666-python-repos-had-comma-typos-including-tensorflow-and-pytorch-sentry-and-v8-7bc3ad9a1bb7