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. Advertising

  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. Advertising

  3. > Why is this?
    It should work. Are you using an old version of Python?
     
    Lonnie Princehouse, Dec 21, 2004
    #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. Michal Mikolajczyk
    Replies:
    1
    Views:
    811
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    960
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    615
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    824
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    970
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page