Tuple Question

Discussion in 'Python' started by VanL, Dec 21, 2004.

  1. VanL

    VanL Guest

    Hello,

    Why is this?

    >>> class MyTuple(tuple):

    .... def __getitem__(self, name):
    .... return tuple.__getitem__(self, name)
    ....
    >>> data = (1,2,3,4,5)
    >>> t = MyTuple(data)
    >>> t[0]

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 3, in __getitem__
    TypeError: descriptor '__getitem__' requires a 'tuple' object but
    received a 'int'

    Thanks,

    VL
     
    VanL, Dec 21, 2004
    #1
    1. Advertisements

  2. VanL wrote:
    > Why is this?
    >
    > >>> class MyTuple(tuple):

    > ... def __getitem__(self, name):
    > ... return tuple.__getitem__(self, name)
    > ...
    > >>> data = (1,2,3,4,5)
    > >>> t = MyTuple(data)
    > >>> t[0]

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "<stdin>", line 3, in __getitem__
    > TypeError: descriptor '__getitem__' requires a 'tuple' object but
    > received a 'int'


    What Python are you using? On Python 2.4:

    >>> class MyTuple(tuple):

    .... def __getitem__(self, name):
    .... return tuple.__getitem__(self, name)
    ....
    >>> data = (1,2,3,4,5)
    >>> t = MyTuple(data)
    >>> t[0]

    1

    Steve
     
    Steven Bethard, Dec 21, 2004
    #2
    1. Advertisements

  3. > Why is this?
    It should work. Are you using an old version of Python?
     
    Lonnie Princehouse, Dec 21, 2004
    #3
    1. Advertisements

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. Michal Mikolajczyk
    Replies:
    1
    Views:
    1,026
    Larry Bates
    Apr 20, 2004
  2. Will McGugan

    Tuple question

    Will McGugan, Sep 2, 2004, in forum: Python
    Replies:
    50
    Views:
    1,460
    Alex Martelli
    Sep 7, 2004
  3. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    1,209
    Antoon Pardon
    Mar 30, 2005
  4. Steve
    Replies:
    1
    Views:
    1,329
    Fredrik Lundh
    Dec 13, 2005
  5. Replies:
    6
    Views:
    339
    Duncan Booth
    Feb 23, 2007
  6. Davy
    Replies:
    3
    Views:
    2,071
    Wildemar Wildenburger
    Nov 7, 2007
  7. Jeff Nyman
    Replies:
    8
    Views:
    668
    Terry Reedy
    Jun 5, 2008
  8. Stuart
    Replies:
    6
    Views:
    391
    Seungbeom Kim
    Mar 11, 2013
Loading...