Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: why does a 1-element tuple in python require comma?

  1. #11
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: why does a 1-element tuple in python require comma?

    Quote Originally Posted by IAMTubby View Post

    As per my understanding, since I think that a "parenthesized 1" is same as a single-element tuple, I still haven't got it completely.
    You are confusing the concept and its notation... A tuple isn't a list of things inside parentheses, it is an immutable sequence of elements. It just happens that its notation uses parentheses, and that in some cases this can be ambiguous with the parentheses as used to group arithmetic expressions.

    The Python designers had to make choices, and decided that one single element surrounded by parentheses would fall in the arithmetic expression grouping realm. They could have chosen otherwise, because there isn't much use for a single operand between parentheses, but that would lead to ambiguous unpacking, because if "(1)" were a tuple, then by symmetry to unpack it you could write:
    Code:
    x = (1)
    and this would prevent the plain assigment of a tuple to a variable:
    Code:
    x = (1)
    So the unambiguous unpacking syntax is:
    Code:
    x, = (1)
    And by symmetry the one-element tuple is written (1,)
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  2. #12
    Join Date
    Aug 2012
    Beans
    623

    Re: why does a 1-element tuple in python require comma?

    Quote Originally Posted by spjackson View Post
    Using the C analogy, 1 is an int and an arrary of one int is not an int, it is an array.
    Oh thanks a lot spjackson.
    You mean to say

    a = (1) is same as doing int a = 1;
    whereas a=(1,) is like int a[] = {1};


    If my above understanding is correct, then it makes sense. I shall mark the thread as solved after I get confirmation on this.

    Hopefully, I'm not dragging this thread too long. But couldn't this have been avoided if a=1;(in python) was enough for int a = 1,(in C) and
    a=(1) was an array(here tuple) ?
    But I know very little python, and I'm sure there must be some reasoning to it.

  3. #13
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: why does a 1-element tuple in python require comma?

    Quote Originally Posted by IAMTubby View Post
    But couldn't this have been avoided if a=1;(in python) was enough for int a = 1,(in C) and
    a=(1) was an array(here tuple) ?
    What makes the tuple is the comma, not the parentheses. Parentheses can be omitted in many cases
    Code:
    x = 1, # creates and assigns a tuple
    x = 1 # assigns an integer
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  4. #14
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: why does a 1-element tuple in python require comma?

    Quote Originally Posted by IAMTubby View Post
    You mean to say

    a = (1) is same as doing int a = 1;
    whereas a=(1,) is like int a[] = {1};


    If my above understanding is correct, then it makes sense. I shall mark the thread as solved after I get confirmation on this.
    Yes, that's about it.
    Hopefully, I'm not dragging this thread too long. But couldn't this have been avoided if a=1;(in python) was enough for int a = 1,(in C) and
    a=(1) was an array(here tuple) ?
    But I know very little python, and I'm sure there must be some reasoning to it.
    Python, in common with many many languages (and math) allows parentheses to be placed around any expression. So we might write for example
    Code:
    >>> a = (((2) + (2)) * (6))
    >>> print(a)
    24
    But a more natural way to write the same thing would be
    Code:
    >>> a = (2 + 2) * 6
    >>> print(a)
    24
    We could simply that still further...
    Code:
    >>> a = (4) * 6
    >>> print(a)
    24
    But by your suggestion, you don't want it to do that, because you want (4) to be a tuple, so you want it to be the same as
    Code:
    >>> a = (4,) * 6
    >>> print(a)
    (4, 4, 4, 4, 4, 4)
    I suggest that would be somewhat confusing.

  5. #15
    Join Date
    Aug 2012
    Beans
    623

    Re: why does a 1-element tuple in python require comma?

    Quote Originally Posted by ofnuts View Post
    The Python designers had to make choices, and decided that one single element surrounded by parentheses would fall in the arithmetic expression grouping realm. They could have chosen otherwise, because there isn't much use for a single operand between parentheses, but that would lead to ambiguous unpacking, because if "(1)" were a tuple, then by symmetry to unpack it you could write:
    Code:
    x = (1)
    and this would prevent the plain assigment of a tuple to a variable:
    Code:
    x = (1)
    So the unambiguous unpacking syntax is:
    Code:
    x, = (1)
    And by symmetry the one-element tuple is written (1,)
    Quote Originally Posted by ofnuts View Post
    What makes the tuple is the comma, not the parentheses. Parentheses can be omitted in many cases
    Code:
    x = 1, # creates and assigns a tuple
    x = 1 # assigns an integer
    Thank you ofnuts, these two points clearly drive home the point.

    Quote Originally Posted by spjackson View Post
    Yes, that's about it.
    Thanks spjackson, comparing with C made understanding easier.

    Allow me to summarise :
    What really makes the tuple is the comma, not the parentheses. x = 1 cannot mean a tuple, so the python developers came up with the syntax of using x = 1, for tuple.
    But Python allows parentheses to be placed around any expression rendering 1 and (1) to be the same thing. Applying this to above. x = (1) is an integer and x = (1,) is a tuple.

    What we talked about so far was assigning or packing. Now looking at unpacking => x = (1) means x is a variable which has the value 1, whereas x, = (1) means x is a single-element tuple which has the value 1. Same would also be true for x = 1 and x, = 1

    So x = (1) is an integer and not a tuple is the main take away.

    Marking the thread as solved.

  6. #16
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: why does a 1-element tuple in python require comma?

    I feel a need to nitpick this a bit further... (1) is not an integer, it is an arithmetic expression that finally evaluates as an integer. This is not so much of a "python allows your to use parentheses" but an "arithmetic expressions are part of most programming languages and they let you use parentheses even in places where you don't strictly need to". See spjackson's example about expression evaluation, it was quite illuminating.
    LambdaGrok. | #ubuntu-programming on FreeNode

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •