Parens do create a tuple (was: one-element tuples [Was: Most probably a stupid question, but I still want to ask])
Ben Finney
ben+python at benfinney.id.au
Sun Apr 10 20:45:08 EDT 2016
Stephen Hansen <me+python at ixokai.io> writes:
> […] parens don't make tuples, commas do.
Chris Angelico <rosuav at gmail.com> writes:
> The thing you're confused at is that it's not the parentheses that
> create a tuple. Parentheses merely group.
MRAB <python at mrabarnett.plus.com> writes:
> As has been said already, it's the comma that makes the tuple. The
> parentheses are often needed to avoid ambiguity.
This is too simplistic, and IMO it's just sowing the seed for future
confusion.
As MRAB states in the same message:
> There _is_ one exception though: (). It's the empty tuple (a 0-element
> tuple). It doesn't have a comma and the parentheses are mandatory.
> There's no other way to write it.
So, let's please stop saying “parens don't create a tuple”. They do, and
because of that I've stopped saying that false over-simplification.
A pair of tuples as an expression is literal syntax to create a tuple
with zero items.
Also, there is another obvious way to create an empty tuple: call the
‘tuple’ type directly:
>>> foo = tuple()
>>> print(type(foo), len(foo))
<class 'tuple'> 0
So the expanation that remains true when you examine it is: People
wanted a literal syntax to create a zero-length tuple. A pair of parens
is that literal syntax, and it's the parens that create the (empty)
tuple.
--
\ “It is hard to believe that a man is telling the truth when you |
`\ know that you would lie if you were in his place.” —Henry L. |
_o__) Mencken |
Ben Finney
More information about the Python-list
mailing list