Could anyone explain why single element tuple is interpreted as that element in Python?
Why don't they just print the tuple
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
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