Why single element tuple is interpreted as that element in python?
Asked Answered
I

6

5

Could anyone explain why single element tuple is interpreted as that element in Python?

And

Why don't they just print the tuple (1,) as (1)?

See the examples below:

>>> (1)
1
>>> ((((1))))
1
>>> print(1,)
1
>>> print((1,))
(1,)
Isidoro answered 20/11, 2016 at 23:0 Comment(4)
Could anyone explain me why single element tuple is interpreted as that element Python? It’s not.Ol
Go back to your interpreter and do this x = (1) and then do type(x).Jampan
@Ol it is not just "it's not". There is a reason: it conflicts with the parenthesis expression (<expr>) where <expr> is 1. To avoid this conflict, for tuples with a single element, the trailing comma is required.Isidoro
@Sang: That was directed at the first part of your question, about why the “single-element tuple” (1) was just 1 (the part that I quoted). The answer is that it’s not the single-element tuple, it’s grouping parentheses.Ol
M
3

It's because (1) is not a tuple. (1) is 1 with parentheses surrounding it. As the python documentation states

it is the comma, not the parentheses, that define the tuple.

Source

The only tuple without a comma is a 0-tuple, which is (). Note, you can check this by running type((1)) and seeing that it returns <type 'int'> not <type 'tuple'>.

Maiga answered 20/11, 2016 at 23:4 Comment(0)
M
2

Because only (1, ) in your examples is tuple. The rest are expressions.

In [4]: type(1,)
Out[4]: int

In [5]: type((1,))
Out[5]: tuple

In [6]: type((1))
Out[6]: int
Massive answered 20/11, 2016 at 23:3 Comment(1)
(1, ) is also an expression (that evaluates to a tuple)Thigh
I
2

This is very detailed answer from @Ray Total

Sure, in general parentheses don't change the meaning of an expression. For example you can say 4+(1) and it will be 5, the same way 4*(2-1) would be 4. Because the convention is to use parentheses for grouping of subexpressions, the designer of Python thought it would be too confusing to overload the meaning to mean both grouping and single-element tuples. Also Python has a type function. In fact type((2)) is int and type((2,)) is tuple. We don't want there to be any ambiguity, which there would be if (2) were treated as a tuple

Isidoro answered 20/11, 2016 at 23:7 Comment(0)
J
2

A single element tuple is never treated as the contained element. Parentheses are mostly useful for grouping, not for creating tuples; a comma does that.

Why don't they just print (1,) as (1)?

Probably because printing a builtin container type gives a representation that can be used to recreate the container object via , say eval:

The docs for __repr__ provides some clarity on this:

If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value

Answering your question, (1) is just integer 1 with a grouping parenthesis. In order to recreate the singleton tuple via its representation, it has to be printed as (1,) which is the valid syntax for creating the tuple.

>>> t = '(1,)'
>>> i = '(1)'
>>> eval(t)
(1,) # tuple
>>> eval(i)
1    # int
Johnsten answered 20/11, 2016 at 23:9 Comment(1)
I thought that it is no more than a CONVENTION. recreate via its representation is very helpfulIsidoro
S
1

A tuple consists of a number of values separated by commas (not necessary parentheses).

You can even define tuple like this

t = 1,    # (with or without surrounding parentheses)
>>> type(t)
<class 'tuple'>

Here , used to tell interpreter to create tuple if not present it will consider as int.

Same rule applies when we define like this

>>> t = (1) 
>>> type(t)
<class 'int'>
Spiteful answered 1/7, 2021 at 14:46 Comment(0)
N
0

(1) is not a tuple, it's just parentheses around a number. This is because sometimes you want to use parentheses to indicate order of operations, for example: (x+y)*z. This obviously doesn't refer to a tuple containing x+y, it's just to show that the addition should happen before the multiplication.

(((1))) is not a tuple for the same reason, the parentheses are just saying "evaluate what's inside before moving on".

print(1,) is just calling the print function on the number 1. When calling a function, a trailing comma is allowed. However, in python2, this will probably pring (1,), because print isn't a function.

print((1,)) is the only thing that prints a tuple, because we are now actually passing a tuple into the function.

Neelon answered 20/11, 2016 at 23:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.