-1

In Python 3.6, x = (1)and type of x is int, it's same as x = 1. So what's the purpose of ()? Why is it not a tuple?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
biao
  • 311
  • 3
  • 10
  • 3
    Brackets can be used around any expressions, like `1 + (1 + 1)`. `1` itself is an expression, so brackets can be used around it. – Sweeper Feb 28 '19 at 07:54
  • 3
    `()` *is* a tuple, an empty one. Non-empty tuples require commas: `1,`. – jonrsharpe Feb 28 '19 at 07:54
  • 2
    Why is this question marked as a duplicate? I don't see any answers in attached question as author doesn't ask how to create a single item tuple, but rather what's the purpose of construction `(1)`, which only @Sweeper answered. @jonrsharpe @paul-rooney @bear-brown @bruno-desthuilliers – deathangel908 Feb 28 '19 at 08:17
  • 1
    @deathangel908 there is no *"purpose of construction"*, `(1)` is `1`. The OP asked why it wasn't a tuple; if they still have questions they're welcome to edit to clarify. Note also you can only ping one user per comment. – jonrsharpe Feb 28 '19 at 08:31
  • `What defines a tuple is not the brackets, it's the commas` really good, my question may be not clear enough, but this is the answer I need, thanks. – biao Feb 28 '19 at 08:52

1 Answers1

1

From the docs:

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).

What defines a tuple is not the brackets, it's the commas, so (1, 2, 3) is a tuple, but it's not because of the brackets, it's because of the commas which means just using 1, 2, 3 would work the same. A tuple with no elements is represented with empty brackets (e.g. ()). A tuple with one element is represented with a value, followed by a comma (e.g. 1,).

NemPlayer
  • 786
  • 1
  • 11
  • 19
  • 2
    *"A tuple with one element always unpacks itself"* - no, without the comma it's *not a tuple to start with*. A one-tuple *doesn't* unpack itself, as your example `x = (1,)` shows; `x` is then a tuple, not the unpacked integer. Also the parentheses are optional there, `x = 1,` works too. – jonrsharpe Feb 28 '19 at 08:03
  • @jonrsharpe Oh man, you're right! I always though that the brackets define a tuple, it's commas that define a tuple, brackets are just so it'd be more easily distinguish. If it wasn't for your comment, I'd always thing that brackets define a tuple. I'll update my answer so it's correct. Thanks! – NemPlayer Feb 28 '19 at 08:07
  • Really? It's in the docs *you quoted from*: *"A tuple consists of a number of values separated by commas... they may be input with or without surrounding parentheses"*. That's *why* 0- and 1-tuples are a *"special problem"*. – jonrsharpe Feb 28 '19 at 08:09
  • I knew what kinds of capabilities a tuple had from other sources and I don't think I've ever heard them specify commas being the crucial part, I never looked at the docs to see how tuples are formed as I though I knew that. I never thought about tuples like that. Now even generator generator comprehensions make sense, the brackets always confused me. I can't believe this went over my head! I guess I just have to look at the docs thoroughly next time I want to post an answer. – NemPlayer Feb 28 '19 at 08:21