10 Replies - 3905 Views - Last Post: 07 May 2011 - 02:45 PM Rate Topic: -----

Poll: Too many commas? (18 member(s) have cast votes)

Should you put an extra comma at the end of each tuple?

  1. Yes (1 votes [5.56%])

    Percentage of vote: 5.56%

  2. No (13 votes [72.22%])

    Percentage of vote: 72.22%

  3. Meh (4 votes [22.22%])

    Percentage of vote: 22.22%

Vote Guests cannot vote

#1 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Too many commas?

Post icon  Posted 06 May 2011 - 11:52 AM

*
POPULAR

With Python, if you want to create a tuple, it must be enclosed in parenthesis. A common problem people run into are 1 element tuples.
#You might think this will work
x = (1)
But it actually makes x equal to 1. To get past this problem, you need only type
x=(1,)
and now you have a tuple containing only 1 value.

This is all well and good, but some programmers take this and adopt the convention to add a comma at the end of every tuple.
x = (1,2,3,4,)


What do you guys think? Is the comma convention good or annoying?

This post has been edited by atraub: 06 May 2011 - 01:38 PM
Reason for edit:: alliteration ftw!


Is This A Good Question/Topic? 6
  • +

Replies To: Too many commas?

#2 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Re: Too many commas?

Posted 06 May 2011 - 12:36 PM

Well, if the syntax is right, I suppose I'm alright with it, but I'm personally pretty anal when it comes to my code, so I'd prefer the look of the no comma myself...
Was This Post Helpful? 0
  • +
  • -

#3 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Too many commas?

Posted 06 May 2011 - 12:53 PM

I don't find them too annoying. In the Django world they are quite common.

What if you have something like
CHOICES = (
    (1, 'foo'),
    (2, 'bar'),
    (3, 'baz')
)


inside your code, but later on you decide to remove the foo and bar options. So you delete those lines. But you forget to insert a comma after the baz line. So you just created a bug. With a trailing comma this cannot happen.
Though I find them a bit annoying in tuples that don't span multiple lines in source code.

This post has been edited by Nallo: 06 May 2011 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Too many commas?

Posted 06 May 2011 - 01:01 PM

Personally, I only use an extra comma when a single value is in a tuple, not for coding clarity, to tell the interpreter it's a tuple value. You need a comma at the end of a single value tuple, and that's why I will only use it then.

It's all a matter of taste - along the same lines you could say:

x = (1, 2, 3)
y = (1, 2, 3,)
z = 1, 2, 3
print x
print y
print z
....
(1, 2, 3)
(1, 2, 3)
(1, 2, 3)



To all mean the same thing.

Interestingly during assignment I tend to use tuples without parens i.e.:
a, b = b, a*b


This post has been edited by Simown: 06 May 2011 - 05:19 PM
Reason for edit:: ftfy

Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Too many commas?

Posted 06 May 2011 - 01:03 PM

During assignment, it's much clearer not to use parens. It's very easy to let yourself forget that those are tuples beneath the surface :)
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Too many commas?

Posted 06 May 2011 - 05:15 PM

atraub, comma chameleon...
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Too many commas?

Posted 06 May 2011 - 08:08 PM

The extra commas annoy me, although Nallo did raise a good point.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Too many commas?

Posted 07 May 2011 - 03:18 AM

First time I had to make a single value tuple, it annoyed me. After that, I got over it. I'm honestly not much bothered either way.

I do appreciate that it's there, though. Being able to do something like:
a = ('Alpha',
	'Bravo',
	'Charlie',
	)



Makes Python that much more helpful.
Was This Post Helpful? 0
  • +
  • -

#9 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Too many commas?

Posted 07 May 2011 - 08:34 AM

I've got to say i think it depends on the situation. If you aren't planning to change anything in the tuple, then you may as well not even bother, if you don't like it.

But, if it is something that you'll change frequently, copy and paste into, etc. you should definatly have it in there. That way, it's always very clear that the entity you are manipulating is a tuple.
Was This Post Helpful? 0
  • +
  • -

#10 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Re: Too many commas?

Posted 07 May 2011 - 09:21 AM

All the tutorials I have followed have used the extra comma so I have too, but I have always wondered why they add it.
Was This Post Helpful? 0
  • +
  • -

#11 Tuishimi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 21-March 09

Re: Too many commas?

Posted 07 May 2011 - 02:45 PM

To me the syntax simply is what it is. It does not bother me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1