bad operand type for unary +: tuple

Discussion in 'Python' started by Frank Millman, Oct 22, 2009.

  1. Hi all

    This is just out of curiosity.

    I have a tuple, and I want to create a new tuple with a new value in the
    first position, and everything else unchanged.

    I figured out that this would work -

    >>> t = ('a', 'b', 'c')
    >>> t2 = ('x',) + t[1:]
    >>> t2

    ('x', 'b', 'c')

    Then I thought I would neaten it a bit by replacing "('x',)" with "'x'," on
    the assumption that it is not necessary to surround a tuple with brackets.

    This is the result -

    >>> t = ('a', 'b', 'c')
    >>> t2 = 'x', + t[1:]

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: bad operand type for unary +: 'tuple'
    >>>


    It is not a problem - I will just stick to using the brackets. However, I
    would be interested to find out the reason for the error.

    Version is 2.6.2.

    Thanks

    Frank Millman
     
    Frank Millman, Oct 22, 2009
    #1
    1. Advertising

  2. Frank Millman wrote:

    > Hi all
    >
    > This is just out of curiosity.
    >
    > I have a tuple, and I want to create a new tuple with a new value in the
    > first position, and everything else unchanged.
    >
    > I figured out that this would work -
    >
    >>>> t = ('a', 'b', 'c')
    >>>> t2 = ('x',) + t[1:]
    >>>> t2

    > ('x', 'b', 'c')
    >
    > Then I thought I would neaten it a bit by replacing "('x',)" with "'x',"
    > on the assumption that it is not necessary to surround a tuple with
    > brackets.
    >
    > This is the result -
    >
    >>>> t = ('a', 'b', 'c')
    >>>> t2 = 'x', + t[1:]

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > TypeError: bad operand type for unary +: 'tuple'
    >>>>

    >
    > It is not a problem - I will just stick to using the brackets. However, I
    > would be interested to find out the reason for the error.
    >
    > Version is 2.6.2.
    >
    > Thanks


    the operator precedence. Sure you want to write

    (a, -b, c)

    to form a tuple with a negated (or actually all other kinds of expressions)
    value in it. So python made -/+ and more or less all other operators
    precede the comma, which is the actual tuple-operator. And consequently,

    a, +(c, d)

    tries to *first* apply + to the tuple (c, d) - which isn't defined.

    Diez
     
    Diez B. Roggisch, Oct 22, 2009
    #2
    1. Advertising

  3. Diez B. Roggisch wrote:
    > Frank Millman wrote:
    >
    >>
    >>>>> t = ('a', 'b', 'c')
    >>>>> t2 = 'x', + t[1:]

    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in <module>
    >> TypeError: bad operand type for unary +: 'tuple'
    >>>>>

    >>


    >
    > the operator precedence. Sure you want to write
    >
    > (a, -b, c)
    >
    > to form a tuple with a negated (or actually all other kinds of
    > expressions)
    > value in it. So python made -/+ and more or less all other operators
    > precede the comma, which is the actual tuple-operator. And consequently,
    >
    > a, +(c, d)
    >
    > tries to *first* apply + to the tuple (c, d) - which isn't defined.
    >


    Makes total sense. Thanks, Diez

    Frank
     
    Frank Millman, Oct 22, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rakesh
    Replies:
    3
    Views:
    6,386
    Rakesh
    Mar 30, 2005
  2. Jay Parlar
    Replies:
    0
    Views:
    814
    Jay Parlar
    Mar 17, 2006
  3. SpOiLeR
    Replies:
    10
    Views:
    848
    SpOiLeR
    Oct 19, 2005
  4. Jean-Michel Pichavant
    Replies:
    0
    Views:
    1,263
    Jean-Michel Pichavant
    Dec 7, 2009
  5. Rami Chowdhury
    Replies:
    0
    Views:
    618
    Rami Chowdhury
    Dec 7, 2009
Loading...

Share This Page