8

In python, one can declare a tuple explicitly with parenthesis as such:

>>> x = (0.25, 0.25, 0.25, 0.25)
>>> x
(0.25, 0.25, 0.25, 0.25)
>>> type(x)
<type 'tuple'>

Alternatively, without parenthesis, python automatically packs its into a immutable tuple:

>>> x = 0.25, 0.25, 0.25, 0.25
>>> x
(0.25, 0.25, 0.25, 0.25)
>>> type(x)
<type 'tuple'>

Is there a pythonic style to declare a tuple? If so, please also reference the relevant PEP or link.

There's no difference in the "end-product" of achieving the tuple but is there a difference in how the tuple with and without parenthesis are initialized (in CPython)?

4
  • 1
    Related: stackoverflow.com/questions/16017811/…. (There isn't any difference - the commas define the tuple, parentheses are optional but often useful.) Commented Jan 15, 2016 at 10:38
  • Whoops wrong it should be CPython Commented Jan 15, 2016 at 10:39
  • Hmm...I'd prefer x = (0.25, 0.25, 0.25, 0.25) because it's clear that x is a tuple. And that's more like a real tuple (I mean, like the output). Commented Jan 15, 2016 at 10:43
  • 1
    Err sorry I had closed it as duplicate of stackoverflow.com/questions/16017811/… but it's not really. And Nikita's answer is the right one. Commented Jan 15, 2016 at 11:36

2 Answers 2

5

From practical point of view it's best to always use parenthesis as it contributes to readability of your code. As one of the import this moto says:

"Explicit is better then implicit."

Also remember, that when defining one-tuple you need a comma: one_tuple = (15, ).

Sign up to request clarification or add additional context in comments.

6 Comments

The point on one element tuple is not correct. x = 1,; x gives (1,)
@alvas: Nikita says that: When defining one-tuple you need a comma. Not about the parenthesis.
@KevinGuan Ah, I misunderstood.
x=1, and x=(1,) both legal tuples, type(x) will give <class 'tuple'>. But x=1 and x=(1) are just integers, type(x) will give <class 'int'> here.
And a zero element tuple needs the parens: ().
|
-1

There's no particular "pythonic" way. Except for the empty parenthesis the parenthesis is just a parenthesis (which controls precedence). That is the expression x = 1,2,3 is same as x = (1,2,3) (as the tuple is created before assignment anyway).

Where it may differ is where the order matters. For example if l is a list or tuple the expression 1,2,l.count(1) would mean that l.count is called before the tuple is created while (1,2,l).count(1) first creates the tuple then it's count is called. It's the same parenthesis as in (2+3)*4 (which says add first and multiply then).

The only case where the parenthesis is required is when creating the empty tuple. This is done by fx the expression x = () because there's no other way. And the only case where trailing comma is mandatory is when creating a tuple of length one (ie x = 1,) since it would be impossible to distinguish from the element 1 otherwise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.